Symfony2无法在EventListener中找到控制器

时间:2013-12-03 02:50:36

标签: php symfony

我正在尝试从EventListener重定向到控制器,但是我收到以下错误消息:

Unable to find controller "HRPortalSystemBundle:Home:login"

我肯定HomeController存在于\ HRPortal \ SystemBundle \ Controller下,并且它确实有一个名为loginAction()的方法。此操作也有一条非常有效的路线:

# routing.yml
login:
    path:    /login
    defaults: { _controller: HRPortalSystemBundle:Home:login }

我的代码如下:

<?php
namespace HRPortal\SystemBundle\EventListener;

use HRPortal\SystemBundle\Controller\TokenAuthenticatedController;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\Controller\ControllerResolver;
use Symfony\Component\HttpFoundation\Request;

class TokenListener
{
    private $em;
    private $userRepo;
    private $session;

    public function __construct($em, $session)
    {
        $this->session = $session;
        $this->em = $em;
        $this->userRepo = $em->getRepository('HRPortalSystemBundle:Users');
    }

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

        if (!is_array($controller)) {
        return;
        }
        if ($controller[0] instanceof TokenAuthenticatedController) {
            if($this->session->has('id') && $this->session->has('token')){
                $sess_id = $this->session->get('id');
                $sess_token = $this->session->get('token');
                $user = $this->userRepo->findBy(array('id'=>$sess_id, 'token'=>$sess_token));
                if($user == null){
                    throw new AccessDeniedHttpException('We could not find the user');
                }else{
                    if($user->token != $sess_token){
                        throw new AccessDeniedHttpException('This action needs a valid token');
                    }
                }
            }else{
                $request = new Request();
                $resolver = new ControllerResolver();
                $request->attributes->set('_controller', 'HRPortalSystemBundle:Home:login');
                $event->setController($resolver->getController($request));
            }
        }
    }
}

另外我不确定$ resolver是否使用得当,因为我猜这将是解决这个问题后的下一个问题。

提前致谢。

===编辑===

我已经完成了以下操作,现在它似乎找到了控制器。

$request = new Request();
$resolver = new ControllerResolver();
$request->attributes->set('_controller', 'HRPortal\SystemBundle\Controller\HomeController::loginAction');
$event->setController($resolver->getController($request));

但是,我收到以下错误消息:

Error: Call to a member function get() on a non-object in /usr/local/apache2/htdocs/hrportal/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php line 106

所以控制器和解析器似乎有问题。

2 个答案:

答案 0 :(得分:2)

在查看了ControllerResolver源代码之后,似乎getController方法希望_controller字符串已经转换为Classname::MethodName格式(或PHP)可调用)。

此转换由ControllerNameParser的{​​{1}}方法完成。

答案 1 :(得分:0)

我终于解决了这个问题。感谢Martin,我使用了ControllerNameParser类来解析Controller名称。但我还需要从services.yml文件中将容器注入到EventListener中。我认为为面临同样问题的开发人员发布代码会很好:

<强> Services.yml:

parameters:
    session_handler.class:    HRPortal\SystemBundle\Services\SessionHandler
    token_listener.class:  HRPortal\SystemBundle\EventListener\TokenListener
    ...

services:
    session_handler:
        class:            "%session_handler.class%"
        arguments:
            em: @doctrine.orm.entity_manager
            session: @session
    tokens_listener:
        class:            "%token_listener.class%"
        arguments:
            em: @doctrine.orm.entity_manager
            container: @service_container
        tags:
            - { name: kernel.event_listener, event: kernel.controller, method: onKernelController }
    ...

<强> TokenListener.php

<?php
namespace HRPortal\SystemBundle\EventListener;

use HRPortal\SystemBundle\Controller\TokenAuthenticatedController;

use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpFoundation\Request;

use Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser;

class TokenListener extends Controller
{
    protected $em;
    protected $userRepo;
    protected $container;

    public function __construct($em, $container)
    {
        $this->container = $container;
        $this->em = $em;
        $this->userRepo = $em->getRepository('HRPortalSystemBundle:Users');
    }

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

        if (!is_array($controller)) {
            return;
        }
        if ($controller[0] instanceof TokenAuthenticatedController) {
            $session = $this->container->get('session_handler');
            $loggedIn = $session->isLoggedIn();
            if(!$loggedIn){
                $request = new Request();
                $request->attributes->set('_controller', 'HRPortal\SystemBundle\Controller\AuthController::loginAction');
                $parser = new ControllerNameParser($this->container->get('kernel'));
                $resolver = new ControllerResolver($this->container, $parser);
                $event->setController($resolver->getController($request)); 
            }
            return;
        }
    }
}