English 中文(简体)
在控制器内调用控制器控制器
原标题:Calling controller inside a controller
  • 时间:2012-05-23 08:05:07
  •  标签:
  • symfony

为什么在控制器内调用控制器时会出现以下错误?

Fatal error: Call to a member function get() on a non-object in /home/web/project/symfony2/vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php on line 149

在控制器中,我调用了一个包含 current 的类 :

class DemoController extends Controller
{
    public function indexAction()
    {
        $object = new AcmeDemoBundleServiceObject();
        $object->method();
    }
    // ...
}

这门课是这样的:

# Acme/DemoBundle/Service/Object.php
class Object extends Controller
{
    public function method()
    {
        $em = $this->getDoctrine()->getEntityManager(); // the problem
        // ...
    }
}

当我使用 < code>$ this 调用服务、 学说或控制器内类似的东西时, 错误就发生了。 否则, 它就会起作用 。

比如说,我该如何使用这个班级的学说呢?

最佳回答

尝试

$object->setContainer($this->container);

在调用 method () 之前

Edit: Basically it s a bad idea to have a service extend Controller but if you really need to do this, try to add this

your.service:
  class: YourClass
  arguments: [...]
  calls:
    - [ setContainer, [@service_container] ]

在您的服务配置文件中( 可能 < code> service.yml )

问题回答

暂无回答




相关问题
Symfony 5, I can t start the server

I develop a symfony project and everything was fine. But since yesterday I can not launch the server: enter image description here The commands symfony server:start and symfony server:stop don t work ...

learning symfony 1.4 will be good when using symfony 2.0?

I know that the architecture is different in symfony 2.0 but im learning 1.4 right now. I wonder if this knowledge i gain about 1.4 will be usable for 2.0 in some extent or will it be a total waste ...

Is symfony 2.0 stable enough to use? [closed]

I wonder if Symfony 2.0 is stable enough to use? Because I ve never used Symfony before. It seems that Symfony 2 is much better than the previous version and I don t want to relearn/recode ...

your experience using symfony 2.0 [closed]

I m going to start a new project that s building web apps from scratch. I have been thinking about using symfony framework for this project. Should I start using symfony 2.0 or stick with 1.4 ? I ...

How to increase the session timeout in Symfony

I would like to know how to increase the session timeout in symfony. Is it enough to only adjust the symfony configuration settings or must I also configure anything in my php.ini file?

How stable or unstable is symfony 2.0? [closed]

Well, I know it s a preview, and I know it says that it s not yet ready for production, and yet I dare ask the question. I need to start building a pretty big application, which is planned to go live ...

热门标签