Symfony2 控制器中的构造函数 [英] Constructor in Symfony2 Controller

查看:19
本文介绍了Symfony2 控制器中的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I define a constructor in Symfony2 controller. I want to get the the logged in user data available in all the methods of my controller, Currently I do something like this in every action of my controller to get the logged in user.

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

我想在构造函数中执行一次,并使该登录用户可用于我的所有操作

I want to do it once in a constructor and make this logged in user available on all my actions

推荐答案

对于在每个控制器操作之前执行代码的一般解决方案,您可以将事件侦听器附加到 kernel.controller 事件,如下所示:

For a general solution for executing code before every controller action you can attach an event listener to the kernel.controller event like so:

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

然后在您的 BeforeControllerListener 中,您将检查控制器以查看它是否实现了一个接口,如果实现了,您将从该接口调用一个方法并传入安全上下文.

Then in your BeforeControllerListener you will check the controller to see if it implements an interface, if it does, you will call a method from the interface and pass in the security context.

<?php

namespace AppCoreBundleEventListener;

use SymfonyComponentHttpKernelEventFilterControllerEvent;
use SymfonyComponentSecurityCoreSecurityContextInterface;
use AppCoreBundleModelInitializableControllerInterface;

/**
 * @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);
        }
    }
}

然后,您希望用户始终可用的任何控制器,您只需实现该接口并像这样设置用户:

Then, any controllers that you want to have the user always available you will just implement that interface and set the user like so:

use AppCoreBundleModelInitializableControllerInterface;

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 AppCoreBundleModel;

use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentSecurityCoreSecurityContextInterface;

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

这篇关于Symfony2 控制器中的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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