Reputation: 485
What I wonder to do is that I have an admin panel which I can access with 'admin' prefix but it's accessible only if you go toward the configuration page first.
For that aim, I've created a Listener event as follow:
<?php
namespace Config\ConfigBundle\Listener;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
class ConfigListener {
public function __construct(ContainerInterface $container){
$this->router = $container->get('router');
$this->em = $container->get('doctrine')->getEntityManager();;
}
public function onKernelRequest(GetResponseEvent $event)
{
$route = $event->getRequest()->attributes->get('_route');
if ( $route == 'admin') {
$config = $this->em->getRepository('ConfigBundle:Config')->findConfig();
if($config == null){
$event->setResponse(new RedirectResponse($this->router->generate('adminConfig')));
}
}
}
}
This code works fine, but, it only get the route named 'admin', and what I want is to check the prefix of this route if it's equal to 'admin' to redirect toward the config page.
I am missing somthing, and I don't know how to resolve that issue...
Upvotes: 1
Views: 1386
Reputation: 4345
You can check if route contains 'admin' prefix using following snippet:
if (0 == strpos($route, 'admin')) {
// perform redirect
}
Upvotes: 1