Мне нужно удалить / {_ locale} из URL для примера, если тип пользователя
127.1.1.0 / en / результат должен быть 127.1.1.0/
127.1.1.0 / en/ contact результат должен быть 127.1.1.0/contact
У меня есть функция в моем контроллере, подобная этой
/**
* @Route("/", name="shop_homepage", options={"expose"=true})
*/
public function indexAction(Request $request)
{
$locale = $request->getLocale();
//var_dump($request->getSession()->get('_locale'));
//throw new NotFoundHttpException('you are using unavailable langage');
return $this->render('FrontBundle:Default:index.html.twig',
$this->container->get('builder.template')->init('index', $locale));
}
, и у меня есть Слушатель, который я настраиваю с помощью сеанса
<?php
namespace FrontBundle\EventListener;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RouterInterface;
class LocaleListener implements EventSubscriberInterface
{
/**
* @var Symfony\Component\Routing\RouterInterface
*/
private $router;
/**
* @var routeCollection \Symfony\Component\Routing\RouteCollection
*/
private $routeCollection;
/**
* @var string
*/
private $defaultLocale;
private $em;
public function __construct($defaultLocale,EntityManager $em,RouterInterface $router)
{
$this->routeCollection = $router->getRouteCollection();
$this->router = $router;
$this->em = $em;
$this->defaultLocale = $defaultLocale;
}
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
$path = $request->getPathInfo();
$rootPath = $request->getSchemeAndHttpHost();
$baseUrl = $event->getRequest()->getBaseUrl();
//
if (!$request->hasPreviousSession()) {
return;
}
// try to see if the locale has been set as a _locale routing parameter
if ($request->getSession()->has('_locale')) {
$locale=$request->getSession()->get('_locale');
$request->setLocale($locale);
//($request->getSession()->setLocale($locale);
} else {
// get the browser locale and substr it
$locale = substr($request->getPreferredLanguage(), 0, 2);
$language = $this->em->getRepository('TranslationBundle:Language')->findOneBy(array(
'locale' => $locale
));
if (!is_object($language)) {
$infos = $this->em->getRepository('AdminBundle:Infos')->getFirstInfo();
$locale = $infos->getDefaultlng()->getLocale();
}
// end it
$this->defaultLocale=$locale;
$request->getSession()->set('_locale', $this->defaultLocale);
$request->setLocale($request->getSession()->get('_locale'));
}
//var_dump($request->get('_locale'));
// $admin=preg_match('/[a-z]{2}\/admin/', $request->getUri());
// $front=preg_match('/[a-z]{2}\/[a-z]/', $request->getUri());
/*if (preg_match('/\/$/', $request->getUri())) {
$url = substr($request->getUri(), 0, -1);
$response = new RedirectResponse($url);
$event->setResponse($response);
}*/
/*if ($request->attributes->get('_route')=='shop_homepage_locale') {
$url = $this->router->generate('shop_homepage');
$response = new RedirectResponse($url);
$event->setResponse($response);
}*/
/*$route_exists_locale = false;
$route_exists_without_locale = false;
foreach($this->routeCollection as $routeObject){
$routePath = $routeObject->getPath();
if($routePath == "/".$request->getSession()->get('_locale').$path){
$route_exists_locale = true;
break;
}else if($routePath==$path){
$route_exists_without_locale = true;
break;
}
}
// var_dump("/".$request->getSession()->get('_locale').$path);
/*$admin=preg_match('/[a-z]{2}\/admin/', $request->getUri());
//var_dump($admin==false);
if($route_exists_without_locale==false && $admin==false){
var_dump($rootPath."/".$baseUrl.$path);
$event->setResponse(new RedirectResponse($rootPath."/".$baseUrl.$path));
}*/
}
public static function getSubscribedEvents()
{
return array(
// must be registered before the default Locale listener
KernelEvents::REQUEST => array(array('onKernelRequest', 20)),
);
}
}
мне нужна твоя помощь, спасибо