3

新しい Silex プロジェクトを始めたばかりです。Cartalyst Sentry 認証パッケージを使用しており、コントローラー サービス コントローラーに挿入したいと考えています。これは、Pimple を拡張する Silex の組み込み依存関係コンテナーを使用する私の試みです。私が物事を正しい方法で行っているかどうか、および何を改善できるかについてのフィードバックが欲しいだけです.

$app['sentry'] = $app->share(function() use ($app) {
    $hasher = new Cartalyst\Sentry\Hashing\NativeHasher;
    $userProvider = new Cartalyst\Sentry\Users\Eloquent\Provider($hasher);
    $groupProvider = new Cartalyst\Sentry\Groups\Eloquent\Provider;
    $throttleProvider = new Cartalyst\Sentry\Throttling\Eloquent\Provider($userProvider);
    $session = new Cartalyst\Sentry\Sessions\NativeSession;
    $cookie = new Cartalyst\Sentry\Cookies\NativeCookie(array());

    $sentry = new Cartalyst\Sentry\Sentry(
        $userProvider,
        $groupProvider,
        $throttleProvider,
        $session,
        $cookie
    );

    Cartalyst\Sentry\Facades\Native\Sentry::setupDatabaseResolver(new PDO(
        $app['db.dsn'], 
        $app['db.options']['user'], 
        $app['db.options']['password']
    ));

    return $sentry;
});

コントローラーの定義:

// General Service Provder for Controllers
$app->register(new Silex\Provider\ServiceControllerServiceProvider());

$app['user.controller'] = $app->share(function() use ($app) {
    return new MyNS\UserController($app);
});

$app->get('/user', "user.controller:indexAction");

これが私のコントローラーです。 app['sentry'] は、コンストラクターに注入することでコントローラーで使用できることに注意してください。

class UserController
{
    private $app;

    public function __construct(Application $app)
    {
        $this->app = $app;
    }

    public function indexAction()
    {
            // just testing various things here....
        $user = $this->app['sentry']->getUserProvider()->findById(1);
        $sql = "SELECT * FROM genes";
        $gene = $this->app['db']->fetchAssoc($sql);
        $this->app['monolog']->addDebug(print_r($gene,true));
        return new JsonResponse($user);
    }

}
4

1 に答える 1

0

これは、使用したいベンダー パッケージを見つけた後のプロセスです。サービス プロバイダーの設定に集中するために、より単純なライブラリを使用しています。

composer 経由で新しいパッケージをインストールします。

~$ composer require ramsey/uuid

サービス プロバイダーを作成します。

<?php

namespace My\Namespaced\Provider;

use Silex\Application;
use Silex\ServiceProviderInterface;
use Rhumsaa\Uuid\Uuid;

class UuidServiceProvider implements ServiceProviderInterface
{
    public function register(Application $app)
    {
        $app['uuid1'] = $app->share(function () use ($app) {
            $uuid1 = Uuid::uuid1();
            return $uuid1;
        });        

        $app['uuid4'] = $app->share(function () use ($app) {
            $uuid4 = Uuid::uuid4();
            return $uuid4;
        });
    }

    public function boot(Application $app)
    {
    }
}

サービスプロバイダーを登録します。

$app->register(new My\Namespaced\Provider\UuidServiceProvider());

コントローラーで新しいサービスを使用します。

<?php

namespace My\Namespaced\Controller;

use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class ExampleController
{
    public function indexAction(Application $app, Request $request)
    {
        $uuid = $app['uuid4']->toString();
        return new Response('<h2>'.$uuid.'</h2>');
    }
}
于 2015-09-06T21:31:53.070 に答える