问题描述
我需要从另一个控制器内的不同控制器访问一个方法.我该怎么做?我可以使用 this->get
方法吗?
I need to access a method from a different controller inside another controller. How can I do it? Can I use this->get
method?
我可以将控制器包含在当前控制器中并为其创建一个对象并通过该对象访问该方法吗?这样做可以"吗?
Can I include the controller inside my current controller and make a object of it and access the method via the object? Is it "ok" to do it this way?
我想调用另一个控制器的表单方法---newAction.
I want to call the form method --- newAction of the other controller.
推荐答案
您可以将您的控制器定义为服务,然后在另一个控制器中获取它.
You can define your controller as service, then get it in another controller.
在您的 services.yml
中将需要的控制器定义为服务:
In your services.yml
define needed controller as a service:
services:
your_service_name:
class: YourCompanyYourBundleControllerYourController
然后在任何控制器中,您都可以通过容器获取此服务:
Then in any controller you'll be able to get this service via container:
$yourController = $this->get('your_service_name');
文档中有一些关于控制器即服务的有用信息
这篇关于如何从控制器内部访问不同的控制器 Symfony2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!