簡體   English   中英

使用 lumen 和 neo4j 創建 api

[英]Create an api using lumen and neo4j

我想使用lumen創建一個 api rest 它將與 neo4j 通信,為此我使用NeoEloquent 我已經閱讀了 NeoEloquent 的文檔,但我很困惑。 我已經了解流明如何與關系數據庫一起工作,有一個模型,一個控制器,我想在我的數據庫上執行的每個操作都通過指定要使用的方法的路由,但我不明白這是怎么回事使用圖形數據庫。 特別是我不明白如何使用 Http 方法創建新標簽、檢索所有標簽和關系。 我嘗試遵循本指南中解釋的相同程序(顯然將其重新調整為我的用例),但沒有成功。

例子

假設我們有兩個具有多對多關系的標簽,這個標簽將是 Exhibit 和 Zone。 我們想要檢索與具有特定標識符的 Exhibit 相關聯的區域。 因此,查詢將是這樣的:

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類並調用retrieveZone函數。 這是控制器類中存在的內容:

<?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函數時,我們也會調用Exhibit模型中存在的函數findZone

<?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();
        }
    }

區域類:

<?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 Internal Server Error

堆棧跟蹤

 [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 在您的模型之間建立連接,您還需要定義“相關”模型,使用行$this->belongsToMany('Zone', 'belongs_to'); 指定該類與另一個類(即圖數據庫中的一個節點)相關並通過關系連接。

為了解決您的問題,您需要至少使用以下內容指定此類:

<?php

namespace App;

use Vinelab\NeoEloquent\Eloquent\Model;
use Vinelab\NeoEloquent\Facade\Neo4jSchema;

class Zone extends Model{}

此外,我建議將belongsToManyZone()重命名為zones()以提高代碼的可讀性,因為這樣您就可以執行以下操作來獲取Exhibit所有Zones

$result = Exhibit::findZone($exhibit_id);
$zones = $result->zones

否則它會是$result->belongsToManyZone讀起來$result->belongsToManyZone

也嘗試改變$this->belongsToMany('App\\Zone', 'belongs_to'); 因為這將確保正確的命名空間和類將被使用並且可以被找到。

我希望這能解決您的問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM