symfony从网址中删除/ locale

时间:2019-01-23 13:57:43

标签: php regex symfony symfony-3.3

我需要从网址中删除/ {_ locale} 例如用户类型

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)),
        );
    }
}

我需要您的帮助,谢谢

0 个答案:

没有答案