1

私が持っているもの:

# 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
...

では、Base Controller に silex $app を注入するには、どの方法が良いでしょうか?

4

1 に答える 1

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 ... )
    }
}

それはかなりうまくいきます。また、PHPをどこかにキャッシュするか、パフォーマンスの問題が必要な場合を除いて、ルーティングにYMLを使用することはお勧めしません。

于 2013-02-07T14:54:34.857 に答える