Symfony3.3:自动装配带有标量参数的控制器 [英] Symfony3.3: autowire a controller with scalar argument

查看:21
本文介绍了Symfony3.3:自动装配带有标量参数的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的控制器包含一个带有标量依赖项 $bugReportRecipient 的操作.

The my controller contains an action with an scalar dependency $bugReportRecipient.

class DefaultController
{
    public function bugReportAction(SwiftTwigMailer $swiftTwigMailer, string $bugReportRecipient, Request $request)
    {
        // more code
    }
}

我的服务定义如下所示:

My service definition looks like this:

AppBundle\Controller\DefaultController:
    autowire: true
    tags: ['controller.service_arguments']

如果我运行该操作,则会显示此错误:

If I run the action this error is shown:

Controller "AppBundle\Controller\DefaultController::bugReportAction()" 要求您为 "$bugReportRecipient" 参数提供一个值.要么参数可为空且未提供空值、未提供默认值或因为在此参数之后存在非可选参数.

如何注入这个参数?到目前为止,我还没有在 symfony 文档中找到任何有用的信息.

How can I inject this argument? I haven't found any helpful in the symfony documentation so far.

:

参数 $bugReportRecipient 是在我的 parameters.yml 中定义的参数.

The argument $bugReportRecipient is parameter which is defined in my parameters.yml.

推荐答案

action 的值和以前一样来自同一个地方——你要么在函数定义中设置一个默认值,要么至少有一个由路线,可以这样设置:

The value for the action comes from the same place as before - you either set a default value in the function definition, or at least have a value given by the route, which might be set like such:

* @Route("/bug-report/{bugReportRecipient}", name="bugreport", 
*        defaults={"bugReportRecipient" = "username"})

SwiftTwigMailer 的注入是因为 controller.service_arguments 标签的注入方式与之前的 Request 相同.

The SwiftTwigMailer is injected because of the controller.service_arguments tag in the same way as Request has been before.

注意 - 您仍然可以在@Route 中为未出现在 {URL} 部分中的变量设置变量的默认值 - 但必须在某处设置.

Note - you can still set a variable's default value in the @Route for a variable that does not appear in the {URL} part - but it has to be set somewhere.

我已经使用这种技术根据正在使用的特定路由为变量设置不同的值.

I've used this technique for setting different values for a variable based on the specific route that is being used.

 * @Route("/",         name="homepage_menus", 
 *        defaults={"hasMenus"=true, "partnerLinks"=false})
 * @Route("/partners", name="homepage_partner_footer", 
 *        defaults={"hasMenus"=false,"partnerLinks"=true})

这些变量(它们也已经在函数定义中设置了默认值)都不能出现在 URL 中,而是根据所使用的路由进行设置.

Neither of these variables (they also have defaults already set in the function definition) can appear in the URL, but are set based on the route that is used.

如果 $bugReportRecipient 是一个参数,它可以像这样在服务定义中设置:

If the $bugReportRecipient is a parameter, it can be set in the service definition like such:

arguments:
    $bugReportRecipient: "%kernel.environment%" # or whatever

似乎没有办法在操作级别设置它,因此您必须使控制器成为更正式的服务,并且至少在类级别设置此参数.

There does not appear to be a way to set it on the action level, and so you will have to make the controller a more formal service, and set at least this parameter on the class-level.

您也可以使用表达式语言来执行此操作.

You may also be able to use the Expression language to do this.

services:
    AppBundle\Mailer:
        arguments: ["@=container.hasParameter('some_param') ? parameter('some_param') : 'default_value'"]

这篇关于Symfony3.3:自动装配带有标量参数的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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