如何从控制器内部访问另一个控制器Symfony2 [英] How to access a different controller from inside a controller Symfony2

查看:85
本文介绍了如何从控制器内部访问另一个控制器Symfony2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从另一个控制器内部的另一个控制器访问方法.我该怎么做?我可以使用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: YourCompany\YourBundle\Controller\YourController

然后在任何控制器中,您都可以通过容器获取此服务:

Then in any controller you'll be able to get this service via container:

$yourController = $this->get('your_service_name');


在文档中有一些关于控制器即服务的有用信息


There is some useful information about Controllers as Services in documentation

这篇关于如何从控制器内部访问另一个控制器Symfony2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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