在每个URL之前执行功能,然后在Symfony 2.6中重定向

时间:2015-08-10 03:57:37

标签: php symfony

我想在处理每个URL之前检查一些用户元值。在此过程中,我想跳过一些特殊的URL,例如登录,登录,当然还需要获得当前用户。

注意:我想检查其他功能并在传递到控制器之前重定向。不想更改响应但请求。希望这是正确的事件。

我目前正在kernel.request事件中挂钩并做这样的事情

创建订阅者

profile_check_subscriber:
        class: BBT\MainBundle\EventListener\ProfileCheckSubscriber
        arguments: ['@user_manager', '@router.default']
        tags:
          - { name: kernel.event_subscriber } 

然后在ProfileCheckSubscriber类中

class ProfileCheckSubscriber implements EventSubscriberInterface {

/**
 * @var UserManager
 */
private $userManager;
/**
 * @var Router
 */
private $router;

public function __construct(UserManager $userManager, Router $router)
{
    $this->userManager = $userManager;
    $this->router = $router;
}

public static function getSubscribedEvents()
{
    return array(
        'kernel.request' => 'onKernelRequest'
    );
}

public function onKernelRequest( GetResponseEvent $event)
{

    $request_uri = $event->getRequest()->getRequestUri();

    $logout_url = $this->router->generate('fos_user_security_logout');

    //don't proceed on these urls
    $check_url_array = array(
        $logout_url,
        $this->router->generate('user_welcome'),
        $this->router->generate('fos_user_registration_confirmed'),
        $this->router->generate('fos_user_registration_register'),
        $this->router->generate('fos_user_security_check'),
        $this->router->generate('fos_user_security_login')
    );

    if( $event->getRequest()->getMethod() !== 'GET' )
        return;

    if( $event->getRequest()->isXmlHttpRequest() )
        return;

    if( in_array( $request_uri, $check_url_array ) )
        return;

    if( false !== strpos( $request_uri, 'f/f_edit' ) )
        return;

    if( false !== strpos( $request_uri, 'register' ) )
        return;

    if( 'anon.' == $this->userManager->current_user || false == $this->userManager->current_user )
        return;

    //good to process

    $forced_profile_page_url = $this->router->generate('user_forced_pages', array('page_type' => 'profile_picture'));
    $profile_fields_url = $this->router->generate('user_profile_edit');

    $profile_image_check = $this->userManager->getSingleMeta('force_profile_image_set');

    $profile_fields_check = $this->userManager->getSingleMeta('force_profile_fields_set');

    if( true == $profile_image_check  && $request_uri != $forced_profile_page_url ){
        $event->setResponse(new RedirectResponse( $forced_profile_page_url ));
    } elseif( true == $profile_fields_check && $request_uri != $profile_fields_url && $request_uri !== $forced_profile_page_url ) {
        $event->setResponse(new RedirectResponse( $profile_fields_url ));
    }

}

}

此处userManager是我的客户,其中current_user由安全令牌检索。

如果我使用kernel.response并使用FilterResponseEvent代替GetResponseEvent但是无法在kernel.request上运行,则此代码可以正常运行,因为它无法找到当前登录的用户。 我不确定kernel.request是如何弄乱会议的。

更新 对不起,伙计们,回到绘图板。设置优先级并没有得到用户。所以仍然无法得到用户。尝试优先级0和50.没有运气!

1 个答案:

答案 0 :(得分:1)

您是否尝试更改收听kernel.controller

的事件
public function onKernelController(FilterControllerEvent $event)
{
    $user = $this->tokenStorage->getToken()->getUser();
    /* all your logic */
    $redirectUrl = /** something **/
    $event->setController(function() use ($redirectUrl) {
        return new RedirectResponse($redirectUrl);
    });
}

http://symfony.com/doc/current/components/http_kernel/introduction.html#the-kernel-controller-event