Symfony2 - 如何在控制器中使用 __construct() 并访问 Securty.Context? [英] Symfony2 - How to use __construct() in a Controller and access Securty.Context?

查看:25
本文介绍了Symfony2 - 如何在控制器中使用 __construct() 并访问 Securty.Context?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 Symfony2 时遇到了一些问题.即如何使用 __construct() 函数.官方文档非常糟糕!

I am having some trouble with Symfony2. Namely in how to use the __construct() function. the Official Documentation is shockingly bad!

我希望能够使用以下内容:

I want to be able to use the following:

public function __construct()
{
    parent::__construct();
    $user = $this->get('security.context')->getToken()->getUser();
}

我如何得到以下错误:

致命错误:无法在第 11 行的/Sites/src/DEMO/DemoBundle/Controller/Frontend/HomeController.php 中调用构造函数

Fatal error: Cannot call constructor in /Sites/src/DEMO/DemoBundle/Controller/Frontend/HomeController.php on line 11

第 11 行是parent::__construct();"

Line 11 is "parent::__construct();"

我删除它并得到以下新错误

I removed it and got the following, new error

致命错误:在第 242 行的/Sites/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php 中的非对象上调用成员函数 get()

Fatal error: Call to a member function get() on a non-object in /Sites/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php on line 242

认为我可能需要设置ContainerInterface DIC,但我不知道如何做到这一点(我尝试过但失败了,很惨)

I think I might need to set up the ContainerInterface DIC, but I have no idea how to do this (I tried and failed, miserably)

大家有什么想法吗?

更新 - 尝试更改以扩展 ContainerAware 并收到此错误:

Update - Tried changing to extend ContainerAware and got this error:

致命错误:第 43 行/Sites/src/DEMO/DemoBundle/Controller/Frontend/HomeController.php 中的接口 Symfony\Component\DependencyInjection\ContainerAwareInterface 无法扩展类 DEMO\DemoBundle\Controller\Frontend\HomeController

Fatal error: Class DEMO\DemoBundle\Controller\Frontend\HomeController cannot extend from interface Symfony\Component\DependencyInjection\ContainerAwareInterface in /Sites/src/DEMO/DemoBundle/Controller/Frontend/HomeController.php on line 43

在控制器中使用以下代码:

Using the following code in the controller:

<?php

namespace DEMO\DemoBundle\Controller\Frontend;

use Symfony\Component\DependencyInjection\ContainerAware;

class HomeController extends ContainerAwareInterface
{
     protected $container;

     public function setContainer(ContainerInterface $container = null)
     {
         $this->container = $container;
     }

推荐答案

我假设您正在扩展默认的 Symfony 控制器?如果是这样,看看代码就会发现答案:

I'm assuming you are extending the default Symfony controller? If so, a look at the code will reveal the answer:

namespace Symfony\Bundle\FrameworkBundle\Controller;

use Symfony\Component\DependencyInjection\ContainerAware;

class Controller extends ContainerAware
{

请注意,没有定义 Controller::__construct,因此使用 parent::__construct 不会让您到任何地方.如果我们看看 ContainerAware:

Notice that there is no Controller::__construct defined so using parent::__construct will not get you anywhere. If we look at ContainerAware:

namespace Symfony\Component\DependencyInjection;

class ContainerAware implements ContainerAwareInterface
{
    protected $container;
    public function setContainer(ContainerInterface $container = null)
    {
        $this->container = $container;
    }
}

同样,在调用 setContainer 之前,没有构造函数和容器不可用.所以覆盖 setContainer 并将您的逻辑放在那里.或者只是制作一个不扩展基本控制器类的独立控制器,并将您的依赖项直接注入到构造函数中.

Again, no constructor and the container is not available until setContainer is called. So override setContainer and put your logic there. Or else just make a stand alone controller that does not extend the base controller class and inject your dependencies directly into the constructor.

2017 年 8 月更新

Update Aug 2017

在这方面仍然获得了一些点击.如果您真的想在每个控制器之前执行某些操作,请使用内核控制器侦听器.如果您只需要用户,那么当然使用 getUser().并且请不要覆盖 setContainer().在某些情况下,它可以工作,但只会使您的代码变得复杂.

Still getting a few hits on this. If you really want to execute something before each controller then use a kernel controller listener. If all you need is the user then of course use getUser(). And please don't override setContainer(). In some cases it would work but it would just convolute your code.

这篇关于Symfony2 - 如何在控制器中使用 __construct() 并访问 Securty.Context?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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