如何在没有路由的情况下将变量传递给控制器?
How to pass variable to controller without routing?
class HelloController
{
/**
* @Route("/", name="hello")
*/
public function indexAction($name)
{
return new Response($name);
}
}
我想在不使用路由的情况下将变量 $name 传递给 indexAction。
我在文档中发现:
services:
# ...
# explicitly configure the service
AppBundle\Controller\HelloController:
public: true
tags:
# add multiple tags to control
- name: controller.service_arguments
action: indexAction
argument: logger
# pass this specific service id
id: monolog.logger.doctrine
这向我们展示了如何将另一个服务传递给控制器,但是如何传递一个简单的变量?
在里面试试这个 routing.yml:
defaults:
_controller: AppBundle:Hello:index
name: "WhatYouWantToPass"
class HelloController
{
/**
* @Route("/", name="hello")
*/
public function indexAction($name)
{
return new Response($name);
}
}
我想在不使用路由的情况下将变量 $name 传递给 indexAction。
我在文档中发现:
services:
# ...
# explicitly configure the service
AppBundle\Controller\HelloController:
public: true
tags:
# add multiple tags to control
- name: controller.service_arguments
action: indexAction
argument: logger
# pass this specific service id
id: monolog.logger.doctrine
这向我们展示了如何将另一个服务传递给控制器,但是如何传递一个简单的变量?
在里面试试这个 routing.yml:
defaults:
_controller: AppBundle:Hello:index
name: "WhatYouWantToPass"