构造函数Symfony2的控制器(Constructor in Symfony2 Controlle

2019-06-25 17:37发布

我如何定义的Symfony2控制器的构造函数。 我想在我的控制器的所有方法可用的登录用户的数据,目前,我做这样的事情在我的控制器来获取登录用户的每一个动作。

$em = $this->getDoctrine()->getEntityManager("pp_userdata");
$user = $this->get("security.context")->getToken()->getUser();

我想在构造函数中,一旦做到这一点,使这个已登录用户可在我所有的行动

Answer 1:

对于每一个控制器动作,您可以将一个事件侦听器之前执行代码的通用解决方案kernel.controller像这样的事件:

<service id="your_app.listener.before_controller" class="App\CoreBundle\EventListener\BeforeControllerListener" scope="request">
    <tag name="kernel.event_listener" event="kernel.controller" method="onKernelController"/>
    <argument type="service" id="security.context"/>
</service>

然后在你的BeforeControllerListener你会检查控制器,看它是否实现了一个接口,如果这样做,你会从接口调用的方法,并通过在安全环境。

<?php

namespace App\CoreBundle\EventListener;

use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\Security\Core\SecurityContextInterface;
use App\CoreBundle\Model\InitializableControllerInterface;

/**
 * @author Matt Drollette <matt@drollette.com>
 */
class BeforeControllerListener
{

    protected $security_context;

    public function __construct(SecurityContextInterface $security_context)
    {
        $this->security_context = $security_context;
    }

    public function onKernelController(FilterControllerEvent $event)
    {
        $controller = $event->getController();

        if (!is_array($controller)) {
            // not a object but a different kind of callable. Do nothing
            return;
        }

        $controllerObject = $controller[0];

        // skip initializing for exceptions
        if ($controllerObject instanceof ExceptionController) {
            return;
        }

        if ($controllerObject instanceof InitializableControllerInterface) {
            // this method is the one that is part of the interface.
            $controllerObject->initialize($event->getRequest(), $this->security_context);
        }
    }
}

然后,你想有用户总是可用的任何控制器,你一定会实现该接口并设置用户像这样:

use App\CoreBundle\Model\InitializableControllerInterface;

class DefaultController implements InitializableControllerInterface
{
    /**
     * Current user.
     *
     * @var User
     */
    private $user;

    /**
     * {@inheritdoc}
     */
    public function initialize(Request $request, SecurityContextInterface $security_context)
    {
        $this->user = $security_context->getToken()->getUser();
    }
// ....
}

接口不外乎

namespace App\CoreBundle\Model;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\SecurityContextInterface;

interface InitializableControllerInterface
{
    public function initialize(Request $request, SecurityContextInterface $security_context);
}


Answer 2:

我在运行过程中出现有点晚,但在控制你可以访问用户:

$this->getUser();

因为2.1应该是工作



Answer 3:

我的这种做法是:

  1. 创建一个空接口InitializableControllerInterface
  2. 让事件侦听器
namespace ACMEBundle\Event;

use Symfony\Component\HttpKernel\Event\FilterControllerEvent;

class ControllerConstructor
{
    public function onKernelController(FilterControllerEvent $event)
    {
        $controller = $event->getController();
        if (!is_array($controller)) {
            // not a object but a different kind of callable. Do nothing
            return;
        }

        $controllerObject = $controller[0];
        if ($controllerObject instanceof InitializableControllerInterface) {
            $controllerObject->__init($event->getRequest());
        }
    }
}
  1. 在您的控制器添加:
class ProfileController extends Controller implements
        InitializableControllerInterface
{
public function __init()
    {
$this->user = $security_context->getToken()->getUser();
}

而你,也就能够得到在每个动作的$这个 - >用户。

问候



文章来源: Constructor in Symfony2 Controller