新しい 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);
}
}