symfony - FatalErrorException in RedirectionListener.php line 46: Error: Call to a member function getUser() on a non-object

Solution:
Did you try to set theaccess_control
in yoursecurity.yml
?
For example:
security:
#[...]
access_control:
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
# and so on, until:
- { path: ^/restricted, role: ROLE_USER }
# or:
- { path: ^/*, role: IS_AUTHENTICATED_ANONYMOUSLY }
Edit: Don't forget to clear cache after changes made.
Answer
Solution:
I'm just giving you an example so that you can modify it to meet your needs. Apart from listener way of doing it, you should look into security.yml approach in FOS. There are many examples. For example, search foraccess_control
in http://www.inanzzz.com/ which will give you 6 examples to study a bit.
Note: Never inject whole container as service parameter!
A few examples.
- Overriding controller response message with an even listener in symfony
- Testing onKernelController event listener for user requests with phpspec in symfony
SERVICE
services:
application_frontend.event_listener.order_create:
class: Application\FrontendBundle\EventListener\OrderCreateListener
arguments:
- @router
tags:
- { name: kernel.event_listener, event: kernel.controller, method: onKernelController }
LISTENER
namespace Application\FrontendBundle\EventListener;
use Application\FrontendBundle\Controller\OrderController;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Routing\RouterInterface;
class OrderCreateListener
{
private $router;
private $redirectRoute = 'fos_user_security_login';
private $invalidRoutes = ['fos_user_security_login', '...', '...'];
public function __construct(
RouterInterface $router
) {
$this->router = $router;
}
public function onKernelController(FilterControllerEvent $event)
{
$controller = $event->getController();
if (!is_array($controller)) {
return;
}
if (
$event->isMasterRequest() && // If it is user request
$controller[0] instanceof OrderController && // Change it to your controller
$this->isValidRoute($event) !== false
) {
return;
}
$url = $this->router->generate($this->redirectRoute);
$response = new RedirectResponse($url);
$event->setResponse($response);
}
private function isCreateCarMethod(FilterControllerEvent $event)
{
$currentRoute = $event->getRequest()->attributes->get('_route');
if (in_array(currentRoute, $this->invalidRoutes)) {
return false;
}
}
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: the browser (or proxy) sent a request that this server could not understand.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.