Symfony3.3: autowire controller with scalar argument

My controller contains an action with scalar dependency $bugReportRecipient.

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

The definition of my service is as follows:

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

If I run the action, this error will be shown:

Controller "AppBundle\Controller\DefaultController::bugReportAction()" requires that you provide a value for the "$bugReportRecipient" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one.

How can I introduce this argument? So far, I have not found useful information in the symfony documentation.

[EDIT]:

An argument $bugReportRecipientis a parameter that is defined in mine parameters.yml.

+4
source share
1 answer

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

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

SwiftTwigMailer - controller.service_arguments , Request.

. - @Route , {URL}, ​​-.

.

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

( , ) URL-, .


$bugReportRecipient , :

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

, , .

.

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

Source: https://habr.com/ru/post/1678613/


All Articles