Note that there are some explanatory texts on larger screens.

plurals
  1. POSymfony2 Unable to find controller inside an EventListener
    primarykey
    data
    text
    <p>I am trying to redirect to a controller from the EventListener but I get the following error message:</p> <pre><code>Unable to find controller "HRPortalSystemBundle:Home:login" </code></pre> <p>I am affirmative that the HomeController exists under \HRPortal\SystemBundle\Controller and that it does has a method called loginAction(). This action also has a route that works very well:</p> <pre><code># routing.yml login: path: /login defaults: { _controller: HRPortalSystemBundle:Home:login } </code></pre> <p>My code is the following:</p> <pre><code>&lt;?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-&gt;session = $session; $this-&gt;em = $em; $this-&gt;userRepo = $em-&gt;getRepository('HRPortalSystemBundle:Users'); } public function onKernelController(FilterControllerEvent $event) { $controller = $event-&gt;getController(); if (!is_array($controller)) { return; } if ($controller[0] instanceof TokenAuthenticatedController) { if($this-&gt;session-&gt;has('id') &amp;&amp; $this-&gt;session-&gt;has('token')){ $sess_id = $this-&gt;session-&gt;get('id'); $sess_token = $this-&gt;session-&gt;get('token'); $user = $this-&gt;userRepo-&gt;findBy(array('id'=&gt;$sess_id, 'token'=&gt;$sess_token)); if($user == null){ throw new AccessDeniedHttpException('We could not find the user'); }else{ if($user-&gt;token != $sess_token){ throw new AccessDeniedHttpException('This action needs a valid token'); } } }else{ $request = new Request(); $resolver = new ControllerResolver(); $request-&gt;attributes-&gt;set('_controller', 'HRPortalSystemBundle:Home:login'); $event-&gt;setController($resolver-&gt;getController($request)); } } } } </code></pre> <p>Also I am not sure if the $resolver is used properly, as I guess this will be the next problem after solving this one.</p> <p>Thanks in advance.</p> <p>=== EDIT ===</p> <p>I have done the following, and it seems it now finds the controller.</p> <pre><code>$request = new Request(); $resolver = new ControllerResolver(); $request-&gt;attributes-&gt;set('_controller', 'HRPortal\SystemBundle\Controller\HomeController::loginAction'); $event-&gt;setController($resolver-&gt;getController($request)); </code></pre> <p>However, I get the following error message:</p> <pre><code>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 </code></pre> <p>So it seems like there's something wrong with the controller and the resolver.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload