Inject other parameters to an action when using controllers in classes pattern?

巧了我就是萌 提交于 2019-12-25 01:37:51

问题


I have a class called ApplicationDecorator, which inherits Application and adds some often used methods.

At the moment each controller action contains at the beginning a line like

$appDec = new ApplicationDecorator($app);

Is it possible to tell Silex to pass the instance as parameter to the action like it is done for Application and Request?

So it would look like the following:

public function switchAction(ApplicationDecorator $appDec, Request $request) {

I am already using Controllers in classes and want to inject an inherited class of Application.

You can use Request and Silex\Application type hints to get $request and $app injected.

At the moment only Request and Application are supported.
Is there any possibility to extend the possible values?


回答1:


You are looking for controllers in classes:

$app->get('/', 'Igorw\\Foo::bar');

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

namespace Igorw
{
  class Foo
  {
    public function bar(Request $request, Application $app)
    {
        ...
    }
  }
}


来源:https://stackoverflow.com/questions/21629873/inject-other-parameters-to-an-action-when-using-controllers-in-classes-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!