将Silex $ app注入自定义Controller类

时间:2013-02-05 22:13:10

标签: dependencies silex

我有什么:

# index.php
$app = new Silex\Application();

... (loading routes file by YamlFileLoader) ...

$app['routes']->addCollection($loader->load('routes.yml'));
$app->run();

路由:

# routes.yml
home:
    pattern: /
    defaults: { _controller: 'Controllers\DefaultController::indexAction' }

如何访问控制器内的$ app实例? 这很简单:

# controllers/DefaultController.php
class DefaultController
{
    public function indexAction(Request $request, Application $app)
    {
        // this is the place!
        return $app['twig']->render('index.twig');
    }
}

好的,没关系,但有几个原因可以解释为什么这种方法对我来说是不可接受的。我想用经典的方式:

# controllers/DefaultController.php
class DefaultController
{
    public function indexAction()
    {
        return $this->twig->render('index.twig');
    }
}

为了提供这个,我想使用父基类:

# controllers/BaseController.php
class BaseController
{
    protected $twig;

    public function __construct(Application $app)
    {
        $this->twig = $app['twig'];
    }
}

正如您所料:

# controllers/DefaultController.php
class DefaultController extends BaseController
...

那么,哪种方式更适合将silex $ app注入基础控制器?

1 个答案:

答案 0 :(得分:1)

我习惯用你的BaseController做同样的事情。

在Controller类中,定义connect方法,如下所示:

class MyController implements ControllerProviderInterface                                                                                                                                                                                         
{                                                                                                                                                                                                                                             
    protected $app;                                                                                                                                                                                                                           

    public function connect(Application $app)                                                                                                                                                                                                 
    {                                                                                                                                                                                                                                         
        $this->app = $app;                                                                                                                                                                                                                    
        $controller_collection = $app['controllers_factory'];                                                                                                                                                                                 

        $controller_collection->get('/', array($this, 'searchForm'))->bind('user_search_form');

        // DECLARE ALL YOUR ROUTES HERE
    }

    public function searchForm()
    {
        return $this->app['twig']->render("search_form", array ... )
    }
}

效果很好。我也不建议你使用YML进行路由,除非你在某处缓存PHP或者你想要性能问题。