在类模式中使用控制器时向动作注入其他参数? [英] Inject other parameters to an action when using controllers in classes pattern?

查看:25
本文介绍了在类模式中使用控制器时向动作注入其他参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为ApplicationDecorator的类,它继承了Application并添加了一些常用的方法.

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

是否可以告诉 Silex 将实例作为参数传递给操作,就像对 ApplicationRequest 所做的那样?

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) {

我已经在使用 类中的控制器 并希望注入一个继承的 Application 类.

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

您可以使用 Request 和 Silex\Application 类型提示来获取 $request和 $app 注入.

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?

推荐答案

您正在寻找 类中的控制器:

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

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

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

这篇关于在类模式中使用控制器时向动作注入其他参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆