在 Laravel 5.1 中使用“Neoxygen/Neoclient”作为 ServiceProvider+Facade

问题描述 投票:0回答:1

[编辑]:好的,我在测试期间多次更新了这篇文章,现在它正在工作...我在下面留下了正确的代码...[/编辑]

从今天早上开始,我一直在尝试使用“Neoxygen/Neoclient”作为 ServiceProvider 和 Facade 来全新安装 Laravel 5.1

为此,我需要在我的composer.json中添加 "neoxygen/neoclient": "^3.0"

然后我在“app/Providers”中创建了一个新的 ServiceProvider,名为“NeoClientServiceProvider”。

在其register方法中;我已经实例化了连接:

public function register()
{
    $this->app->singleton('neoclient', function ($app) {
        return ClientBuilder::create()
            ->addConnection('default', 'http', env('NEO4J_HOST'), intval(env('NEO4J_PORT')), true, env('NEO4J_USER'), env('NEO4J_PASSWORD'))
            ->setDefaultTimeout( intval(env('NEO4J_TIMEOUT')) )
            ->setAutoFormatResponse(true)
            ->build();
    });
}

接下来,我通过在提供程序中包含完整类并设置别名,在“config/app.php”中注册了 ServiceProvider:

'providers' => [ 
...
App\Providers\NeoClientServiceProvider::class
...
],
'aliases' => [
...
'NeoClient' => App\NeoClient::class
...
]

我还创建了一个 NeoClient 类来扩展 Facade,如下所示:

<?php namespace App;

use \Illuminate\Support\Facades\Facade;

class NeoClient extends Facade
{
/**
 * Get the registered name of the component.
 *
 * @return string
 */
protected static function getFacadeAccessor() { return 'neoclient'; }
}

终于,我有了一个像这样的控制器:

<?php namespace App\Http\Controllers;

use NeoClient;

class GenreController extends Controller
{

public function __construct()
{
    // needed authentication
    //$this->middleware('oauth');
}


public function create()
{
    $data = NeoClient::sendCypherQuery("MATCH (g:Genre) RETURN COUNT(g) AS total")->getRows();
    return response()->json($data);
}

}

PS:我知道“NeoEloquent”存在,但我不想使用这个......

++

弗雷德。

php laravel neo4j neoxygen
1个回答
0
投票

当然可以!这是客户的链接:

https://github.com/graphaware/neo4j-php-client

++

© www.soinside.com 2019 - 2024. All rights reserved.