问题描述
我一直在阅读 Symfony2 文档中的内部章节,它说如果我向 kernel.controller 事件添加一个监听器,我可以交换运行的控制器,我有一些工作有点像这样:
I've been reading through the internals chapter in the Symfony2 docs and it says if I add a listener to the kernel.controller event I can swap the controller that gets run, I've got something that works a bit like this:
public function onKernelController(FilterControllerEvent $event)
{
$controller = $event->getController();
$replacementControllerName = .... //Some logic to work out the name of the new controller
$replacementController = ?? //Not sure what goes here
$event->setController($replacementController);
}
我不确定是不是一旦我确定了替换控制器的名称,我该如何获取它的实例并传递给 setController?
The bit I'm unsure if is once I've worked out the name of the replacement controller, how do I get an instance of it that I can pass to setController?
推荐答案
您可以将您的控制器设置为任何可调用,这意味着类似于
You can set your controller to any callable, which means something like
- 一个静态方法
array('class', 'method')
- 一个实例方法
array($instance, 'method')
- 匿名函数
function() { ... }
- 一个普通的全局函数
'function'
; - 一个实现
__invoke的类的实例()
方法new MyClassImplementingInvoke()
- 特殊语法
'class::method'
强制ControllerResolver
创建一个class
的新实例(不带任何参数调用构造函数)并返回一个可调用的数组($instanceOfClass, '方法')
- A static method
array('class', 'method')
- An instance method
array($instance, 'method')
- An anonymous function
function() { ... }
- A regular global function
'function'
; - An instance of a class implementing the
__invoke()
methodnew MyClassImplementingInvoke()
- The special syntax
'class::method'
which forces theControllerResolver
to create a new instance ofclass
(calling the constructor without any argument) and returning a callablearray($instanceOfClass, 'method')
我查错了ControllerResolver
.在标准设置中运行 Symfony 时,它将使用 Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver
(而不是 Symfony\Component\HttpKernel\Controller\ControllerResolver
).所以控制器名称的处理与我上面写的有点不同.
I looked up the wrong ControllerResolver
. When running Symfony in a standard setup it'll use the Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver
(and not the Symfony\Component\HttpKernel\Controller\ControllerResolver
). So the controller name will be handled a little bit different to what I wrote above.
以下示例总结了设置控制器时所有可能的选项.
The following example sums up all the possible options you have when setting your controller.
public function onKernelController(FilterControllerEvent $event)
{
$controller = $event->getController();
// call method in Controller class in YourBundle
$replacementController = 'YourBundle:Controller:method';
// call method in service (which is a service registered in the DIC)
$replacementController = 'service:method';
// call method on an instance of Class (created by calling the constructor without any argument)
$replacementController = 'Class::method';
// call method on Class statically (static method)
$replacementController = array('Class', 'method');
// call method on $controller
$controller = new YourController(1, 2, 3);
$replacementController = array($controller, 'method');
// call __invoke on $controller
$replacementController = new YourController(1, 2, 3);
$event->setController($replacementController);
}
这篇关于尝试使用带有 Symfony2 的事件侦听器交换控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!