使用 lumen 和 neo4j 创建一个 api
Create an api using lumen and neo4j
我想使用 lumen that it will comunicate with neo4j, for this purpose I'm using NeoEloquent 创建一个 api 休息。
我已经阅读了 NeoEloquent 的文档,但我很困惑。我已经了解流明如何与关系数据库一起工作,有一个模型,一个控制器,我想在我的数据库上执行的每个操作都通过指定要使用的方法的路由传递,但我不明白这是怎么回事使用图形数据库。
特别是我不明白如何使用 Http 方法创建新标签、检索所有标签和关系。
我已尝试按照此 guide 中解释的相同程序(显然将其重新适应我的用例)但没有成功。
例子
假设我们有两个具有多对多关系的标签,这个标签将是 Exhibit 和 Zone。我们想要检索与具有特定标识符的展览相关联的区域。
所以,查询将是这样的:
MATCH (e:Exhibit)-[belongs_to]->(z:Zone) WHERE e.exhibit_id = {exhibit_id} RETURN z
为了执行此查询,我们需要在 web.php 文件中提供此路由:
$router->get('/', function () use ($router) {
return $router->app->version();
});
$router->group(['prefix' => 'api'], function () use ($router) {
$router->get('exhibit', ['uses' => 'ExhibitController@showAllExhibit']);
$router->get('exhibit/{exhibit_id}', ['uses' => 'ExhibitController@retrieveZone']);
});
通过这条路线,我们说:
当使用 get 方法发出请求时,进入 ExhibitController class 并调用 retrieveZone 功能。
这是控制器 class:
中的内容
<?php
namespace App\Http\Controllers;
use App\Exhibit;
use Illuminate\Http\Request;
class ExhibitController extends Controller
{
public function showAllExhibit()
{
return response()->json(Exhibit::all());
}
public function showOneExhibit($id)
{
return response()->json(Exhibit::find($id));
}
public function create(Request $request)
{
$exhibit = Exhibit::create($request->all());
return response()->json($exhibit, 201);
}
public function update($id, Request $request)
{
$exhibit = Exhibit::findOrFail($id);
$exhibit->update($request->all());
return response()->json($exhibit, 200);
}
public function delete($id)
{
Exhibit::findOrFail($id)->delete();
return response('Deleted Successfully', 200);
}
public function retrieveZone($exhibit_id)
{
$result = Exhibit::findZone($exhibit_id);
return response()->json($result,201);
}
}
当我们调用 retrieveZone 函数时,我们将调用函数 findZone 以及 Exhibit 模型中的函数:
<?php
namespace App;
use Vinelab\NeoEloquent\Eloquent\Model;
use Vinelab\NeoEloquent\Facade\Neo4jSchema;
class Exhibit extends Model{
protected $label = 'Exhibit';
protected $fillable = [];
protected $hidden = [];
public function belongsToManyZone(){
return $this->belongsToMany('Zone', 'belongs_to');
}
public static function findZone($exhibit_id){
$exhibit = Exhibit::find($exhibit_id);
return $exhibit->belongsToManyZone();
}
}
区域 class:
<?php
namespace App;
use Vinelab\NeoEloquent\Eloquent\Edges\EdgeIn;
use Vinelab\NeoEloquent\Eloquent\Model;
class Zone extends Model{
protected $label = 'Zone';
protected $fillable = ['name'];
protected $hidden = [];
}
这是我使用 NeoEloquent、Lumen 和 Fastroute 翻译查询所做的,但结果是 500 内部服务器错误。
堆栈跟踪
[2018-10-11 16:37:18] local.ERROR: Symfony\Component\Debug\Exception\FatalThrowableError: Class 'Zone' not found in E:\laravel-projects\api_certose\vendor\vinelab\neoeloquent\src\Eloquent\Model.php:291
Stack trace:
#0 E:\laravel-projects\api_certose\app\Exhibit.php(16): Vinelab\NeoEloquent\Eloquent\Model->belongsToMany('Zone', 'BELONGS_TO')
#1 E:\laravel-projects\api_certose\app\Exhibit.php(22): App\Exhibit->zones()
#2 E:\laravel-projects\api_certose\app\Http\Controllers\ExhibitController.php(44): App\Exhibit::findZone('159')
#3 [internal function]: App\Http\Controllers\ExhibitController->retrieveZone('159')
#4 E:\laravel-projects\api_certose\vendor\illuminate\container\BoundMethod.php(29): call_user_func_array(Array, Array)
#5 E:\laravel-projects\api_certose\vendor\illuminate\container\BoundMethod.php(87): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#6 E:\laravel-projects\api_certose\vendor\illuminate\container\BoundMethod.php(31): Illuminate\Container\BoundMethod::callBoundMethod(Object(Laravel\Lumen\Application), Array, Object(Closure))
#7 E:\laravel-projects\api_certose\vendor\illuminate\container\Container.php(564): Illuminate\Container\BoundMethod::call(Object(Laravel\Lumen\Application), Array, Array, NULL)
#8 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(373): Illuminate\Container\Container->call(Array, Array)
#9 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(339): Laravel\Lumen\Application->callControllerCallable(Array, Array)
#10 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(313): Laravel\Lumen\Application->callLumenController(Object(App\Http\Controllers\ExhibitController), 'retrieveZone', Array)
#11 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(275): Laravel\Lumen\Application->callControllerAction(Array)
#12 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(260): Laravel\Lumen\Application->callActionOnArrayBasedRoute(Array)
#13 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(230): Laravel\Lumen\Application->handleFoundRoute(Array)
#14 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(164): Laravel\Lumen\Application->handleDispatcherResponse(Array)
#15 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(413): Laravel\Lumen\Application->Laravel\Lumen\Concerns\{closure}()
#16 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(166): Laravel\Lumen\Application->sendThroughPipeline(Array, Object(Closure))
#17 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(107): Laravel\Lumen\Application->dispatch(NULL)
#18 E:\laravel-projects\api_certose\public\index.php(28): Laravel\Lumen\Application->run()
#19 {main} {"exception":"[object] (Symfony\Component\Debug\Exception\FatalThrowableError(code: 0): Class 'Zone' not found at E:\laravel-projects\api_certose\vendor\vinelab\
eoeloquent\src\Eloquent\Model.php:291)
[stacktrace]
#0 E:\laravel-projects\api_certose\app\Exhibit.php(16): Vinelab\NeoEloquent\Eloquent\Model->belongsToMany('Zone', 'BELONGS_TO')
#1 E:\laravel-projects\api_certose\app\Exhibit.php(22): App\Exhibit->zones()
#2 E:\laravel-projects\api_certose\app\Http\Controllers\ExhibitController.php(44): App\Exhibit::findZone('159')
#3 [internal function]: App\Http\Controllers\ExhibitController->retrieveZone('159')
#4 E:\laravel-projects\api_certose\vendor\illuminate\container\BoundMethod.php(29): call_user_func_array(Array, Array)
#5 E:\laravel-projects\api_certose\vendor\illuminate\container\BoundMethod.php(87): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#6 E:\laravel-projects\api_certose\vendor\illuminate\container\BoundMethod.php(31): Illuminate\Container\BoundMethod::callBoundMethod(Object(Laravel\Lumen\Application), Array, Object(Closure))
#7 E:\laravel-projects\api_certose\vendor\illuminate\container\Container.php(564): Illuminate\Container\BoundMethod::call(Object(Laravel\Lumen\Application), Array, Array, NULL)
#8 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(373): Illuminate\Container\Container->call(Array, Array)
#9 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(339): Laravel\Lumen\Application->callControllerCallable(Array, Array)
#10 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(313): Laravel\Lumen\Application->callLumenController(Object(App\Http\Controllers\ExhibitController), 'retrieveZone', Array)
#11 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(275): Laravel\Lumen\Application->callControllerAction(Array)
#12 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(260): Laravel\Lumen\Application->callActionOnArrayBasedRoute(Array)
#13 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(230): Laravel\Lumen\Application->handleFoundRoute(Array)
#14 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(164): Laravel\Lumen\Application->handleDispatcherResponse(Array)
#15 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(413): Laravel\Lumen\Application->Laravel\Lumen\Concerns\{closure}()
#16 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(166): Laravel\Lumen\Application->sendThroughPipeline(Array, Object(Closure))
#17 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(107): Laravel\Lumen\Application->dispatch(NULL)
#18 E:\laravel-projects\api_certose\public\index.php(28): Laravel\Lumen\Application->run()
#19 {main}
"}
为了让 NeoEloquent 在您的模型之间建立联系,您还需要定义 'related' 模型,行 $this->belongsToMany('Zone', 'belongs_to');
指定此 class 与另一个 class(即图形数据库中的节点)并与关系连接。
为了解决您的问题,您需要指定此 class 至少包含以下内容:
<?php
namespace App;
use Vinelab\NeoEloquent\Eloquent\Model;
use Vinelab\NeoEloquent\Facade\Neo4jSchema;
class Zone extends Model{}
我还建议将 belongsToManyZone()
重命名为 zones()
以提高代码的可读性,因为这样您就可以执行以下操作来获取 [=16= 的所有 Zones
]:
$result = Exhibit::findZone($exhibit_id);
$zones = $result->zones
否则会是$result->belongsToManyZone
,读起来有点奇怪。
同时尝试更改 $this->belongsToMany('App\Zone', 'belongs_to');
,因为这将确保正确的命名空间,并且 class 将被使用并且可以被找到。
希望这能解决您的问题
我想使用 lumen that it will comunicate with neo4j, for this purpose I'm using NeoEloquent 创建一个 api 休息。 我已经阅读了 NeoEloquent 的文档,但我很困惑。我已经了解流明如何与关系数据库一起工作,有一个模型,一个控制器,我想在我的数据库上执行的每个操作都通过指定要使用的方法的路由传递,但我不明白这是怎么回事使用图形数据库。 特别是我不明白如何使用 Http 方法创建新标签、检索所有标签和关系。 我已尝试按照此 guide 中解释的相同程序(显然将其重新适应我的用例)但没有成功。
例子
假设我们有两个具有多对多关系的标签,这个标签将是 Exhibit 和 Zone。我们想要检索与具有特定标识符的展览相关联的区域。 所以,查询将是这样的:
MATCH (e:Exhibit)-[belongs_to]->(z:Zone) WHERE e.exhibit_id = {exhibit_id} RETURN z
为了执行此查询,我们需要在 web.php 文件中提供此路由:
$router->get('/', function () use ($router) {
return $router->app->version();
});
$router->group(['prefix' => 'api'], function () use ($router) {
$router->get('exhibit', ['uses' => 'ExhibitController@showAllExhibit']);
$router->get('exhibit/{exhibit_id}', ['uses' => 'ExhibitController@retrieveZone']);
});
通过这条路线,我们说: 当使用 get 方法发出请求时,进入 ExhibitController class 并调用 retrieveZone 功能。 这是控制器 class:
中的内容<?php
namespace App\Http\Controllers;
use App\Exhibit;
use Illuminate\Http\Request;
class ExhibitController extends Controller
{
public function showAllExhibit()
{
return response()->json(Exhibit::all());
}
public function showOneExhibit($id)
{
return response()->json(Exhibit::find($id));
}
public function create(Request $request)
{
$exhibit = Exhibit::create($request->all());
return response()->json($exhibit, 201);
}
public function update($id, Request $request)
{
$exhibit = Exhibit::findOrFail($id);
$exhibit->update($request->all());
return response()->json($exhibit, 200);
}
public function delete($id)
{
Exhibit::findOrFail($id)->delete();
return response('Deleted Successfully', 200);
}
public function retrieveZone($exhibit_id)
{
$result = Exhibit::findZone($exhibit_id);
return response()->json($result,201);
}
}
当我们调用 retrieveZone 函数时,我们将调用函数 findZone 以及 Exhibit 模型中的函数:
<?php
namespace App;
use Vinelab\NeoEloquent\Eloquent\Model;
use Vinelab\NeoEloquent\Facade\Neo4jSchema;
class Exhibit extends Model{
protected $label = 'Exhibit';
protected $fillable = [];
protected $hidden = [];
public function belongsToManyZone(){
return $this->belongsToMany('Zone', 'belongs_to');
}
public static function findZone($exhibit_id){
$exhibit = Exhibit::find($exhibit_id);
return $exhibit->belongsToManyZone();
}
}
区域 class:
<?php
namespace App;
use Vinelab\NeoEloquent\Eloquent\Edges\EdgeIn;
use Vinelab\NeoEloquent\Eloquent\Model;
class Zone extends Model{
protected $label = 'Zone';
protected $fillable = ['name'];
protected $hidden = [];
}
这是我使用 NeoEloquent、Lumen 和 Fastroute 翻译查询所做的,但结果是 500 内部服务器错误。
堆栈跟踪
[2018-10-11 16:37:18] local.ERROR: Symfony\Component\Debug\Exception\FatalThrowableError: Class 'Zone' not found in E:\laravel-projects\api_certose\vendor\vinelab\neoeloquent\src\Eloquent\Model.php:291
Stack trace:
#0 E:\laravel-projects\api_certose\app\Exhibit.php(16): Vinelab\NeoEloquent\Eloquent\Model->belongsToMany('Zone', 'BELONGS_TO')
#1 E:\laravel-projects\api_certose\app\Exhibit.php(22): App\Exhibit->zones()
#2 E:\laravel-projects\api_certose\app\Http\Controllers\ExhibitController.php(44): App\Exhibit::findZone('159')
#3 [internal function]: App\Http\Controllers\ExhibitController->retrieveZone('159')
#4 E:\laravel-projects\api_certose\vendor\illuminate\container\BoundMethod.php(29): call_user_func_array(Array, Array)
#5 E:\laravel-projects\api_certose\vendor\illuminate\container\BoundMethod.php(87): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#6 E:\laravel-projects\api_certose\vendor\illuminate\container\BoundMethod.php(31): Illuminate\Container\BoundMethod::callBoundMethod(Object(Laravel\Lumen\Application), Array, Object(Closure))
#7 E:\laravel-projects\api_certose\vendor\illuminate\container\Container.php(564): Illuminate\Container\BoundMethod::call(Object(Laravel\Lumen\Application), Array, Array, NULL)
#8 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(373): Illuminate\Container\Container->call(Array, Array)
#9 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(339): Laravel\Lumen\Application->callControllerCallable(Array, Array)
#10 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(313): Laravel\Lumen\Application->callLumenController(Object(App\Http\Controllers\ExhibitController), 'retrieveZone', Array)
#11 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(275): Laravel\Lumen\Application->callControllerAction(Array)
#12 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(260): Laravel\Lumen\Application->callActionOnArrayBasedRoute(Array)
#13 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(230): Laravel\Lumen\Application->handleFoundRoute(Array)
#14 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(164): Laravel\Lumen\Application->handleDispatcherResponse(Array)
#15 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(413): Laravel\Lumen\Application->Laravel\Lumen\Concerns\{closure}()
#16 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(166): Laravel\Lumen\Application->sendThroughPipeline(Array, Object(Closure))
#17 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(107): Laravel\Lumen\Application->dispatch(NULL)
#18 E:\laravel-projects\api_certose\public\index.php(28): Laravel\Lumen\Application->run()
#19 {main} {"exception":"[object] (Symfony\Component\Debug\Exception\FatalThrowableError(code: 0): Class 'Zone' not found at E:\laravel-projects\api_certose\vendor\vinelab\
eoeloquent\src\Eloquent\Model.php:291)
[stacktrace]
#0 E:\laravel-projects\api_certose\app\Exhibit.php(16): Vinelab\NeoEloquent\Eloquent\Model->belongsToMany('Zone', 'BELONGS_TO')
#1 E:\laravel-projects\api_certose\app\Exhibit.php(22): App\Exhibit->zones()
#2 E:\laravel-projects\api_certose\app\Http\Controllers\ExhibitController.php(44): App\Exhibit::findZone('159')
#3 [internal function]: App\Http\Controllers\ExhibitController->retrieveZone('159')
#4 E:\laravel-projects\api_certose\vendor\illuminate\container\BoundMethod.php(29): call_user_func_array(Array, Array)
#5 E:\laravel-projects\api_certose\vendor\illuminate\container\BoundMethod.php(87): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#6 E:\laravel-projects\api_certose\vendor\illuminate\container\BoundMethod.php(31): Illuminate\Container\BoundMethod::callBoundMethod(Object(Laravel\Lumen\Application), Array, Object(Closure))
#7 E:\laravel-projects\api_certose\vendor\illuminate\container\Container.php(564): Illuminate\Container\BoundMethod::call(Object(Laravel\Lumen\Application), Array, Array, NULL)
#8 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(373): Illuminate\Container\Container->call(Array, Array)
#9 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(339): Laravel\Lumen\Application->callControllerCallable(Array, Array)
#10 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(313): Laravel\Lumen\Application->callLumenController(Object(App\Http\Controllers\ExhibitController), 'retrieveZone', Array)
#11 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(275): Laravel\Lumen\Application->callControllerAction(Array)
#12 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(260): Laravel\Lumen\Application->callActionOnArrayBasedRoute(Array)
#13 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(230): Laravel\Lumen\Application->handleFoundRoute(Array)
#14 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(164): Laravel\Lumen\Application->handleDispatcherResponse(Array)
#15 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(413): Laravel\Lumen\Application->Laravel\Lumen\Concerns\{closure}()
#16 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(166): Laravel\Lumen\Application->sendThroughPipeline(Array, Object(Closure))
#17 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(107): Laravel\Lumen\Application->dispatch(NULL)
#18 E:\laravel-projects\api_certose\public\index.php(28): Laravel\Lumen\Application->run()
#19 {main}
"}
为了让 NeoEloquent 在您的模型之间建立联系,您还需要定义 'related' 模型,行 $this->belongsToMany('Zone', 'belongs_to');
指定此 class 与另一个 class(即图形数据库中的节点)并与关系连接。
为了解决您的问题,您需要指定此 class 至少包含以下内容:
<?php
namespace App;
use Vinelab\NeoEloquent\Eloquent\Model;
use Vinelab\NeoEloquent\Facade\Neo4jSchema;
class Zone extends Model{}
我还建议将 belongsToManyZone()
重命名为 zones()
以提高代码的可读性,因为这样您就可以执行以下操作来获取 [=16= 的所有 Zones
]:
$result = Exhibit::findZone($exhibit_id);
$zones = $result->zones
否则会是$result->belongsToManyZone
,读起来有点奇怪。
同时尝试更改 $this->belongsToMany('App\Zone', 'belongs_to');
,因为这将确保正确的命名空间,并且 class 将被使用并且可以被找到。
希望这能解决您的问题