在Silex应用程序中共享第三方依赖的最佳实践?

时间:2013-05-06 22:14:26

标签: php dependency-injection silex pimple

我刚刚开始一个新的Silex项目。我正在使用Cartalyst Sentry Authentication包,我希望注入我的控制器服务控制器。这是我尝试使用Silex的内置依赖容器扩展Pimple。我想要一些关于我是否以正确方式处理事情以及我可以改进的方式的反馈。

$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);
    }

}

1 个答案:

答案 0 :(得分:0)

这是我找到vendor package后想要使用的过程。我正在使用一个更简单的库,专注于设置service provider

通过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>');
    }
}