Symfony 3将所有路由重定向到当前区域设置版本

时间:2016-01-10 21:37:53

标签: php locale symfony symfony-routing

我正在开发一个symfony应用程序,我的目标是无论用户在哪个页面上都会导航到该页面的语言环境版本。

例如,如果用户导航到" /"主页,它将重定向到" / en /"

如果他们在" / admin"页面将重定向到" / en / admin" ,以便从路径设置_locale属性。

如果他们从用户浏览器访问/ admin,则需要确定区域设置,因为没有确定区域设置,因此它知道要重定向到哪个页面。

目前我的默认控制器如下所示,因为我正在测试。我正在使用开发模式&用于测试翻译是否正确的分析器。

enter image description here

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class DefaultController extends Controller
{
    /**
     * @Route("/", name="homepage")
     * @Route("/{_locale}/", name="homepage_locale")
     */
    public function indexAction(Request $request)
    {
        $translated = $this->get('translator')->trans('Symfony is great');

        // replace this example code with whatever you need
        return $this->render('default/index.html.twig', [
            'base_dir' => realpath($this->container->getParameter('kernel.root_dir').'/..'),
            'translated' => $translated
        ]);
    }
}

此当前方法将使用户保持在&#34; /&#34;如果他们在那里导航,但我想让它重定向到&#34; / en /&#34;。这也适用于其他页面,如/ admin,或/ somepath / pathagain / article1(/ en / admin,/ en / somepath / pathagain / article1)

我该怎么做?

参考文献我读过没有帮助的内容:

Symfony2 Use default locale in routing (one URL for one language)

Symfony2 default locale in routing

::更新::

我还没有解决我的问题,但我已经接近并学会了一些技巧来提高效率。

DefaultController.php

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class DefaultController extends Controller
{

    /**
     * @Route("/", name="home", defaults={"_locale"="en"}, requirements={"_locale" = "%app.locales%"})
     * @Route("/{_locale}/", name="home_locale", requirements={"_locale" = "%app.locales%"})
     */
    public function indexAction(Request $request)
    {
        $translated = $this->get('translator')->trans('Symfony is great');

        // replace this example code with whatever you need
        return $this->render('default/index.html.twig', [
            'base_dir' => realpath($this->container->getParameter('kernel.root_dir').'/..'),
            'translated' => $translated
        ]);
    }

    /**
     * @Route("/admin", name="admin", defaults={"_locale"="en"}, requirements={"_locale" = "%app.locales%"})
     * @Route("/{_locale}/admin", name="admin_locale", requirements={"_locale" = "%app.locales%"})
     */
    public function adminAction(Request $request)
    {
        $translated = $this->get('translator')->trans('Symfony is great');

        // replace this example code with whatever you need
        return $this->render('default/index.html.twig', [
            'base_dir' => realpath($this->container->getParameter('kernel.root_dir').'/..'),
            'translated' => $translated
        ]);
    }
}
?>

Config.yml

imports:
    - { resource: parameters.yml }
    - { resource: security.yml }
    - { resource: services.yml }

# Put parameters here that don't need to change on each machine where the app is deployed
# http://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
    locale: en
    app.locales: en|es|zh

framework:
    #esi:             ~
    translator:      { fallbacks: ["%locale%"] }
    secret:          "%secret%"
    router:
        resource: "%kernel.root_dir%/config/routing.yml"
        strict_requirements: ~
    form:            ~
    csrf_protection: ~
    validation:      { enable_annotations: true }
    #serializer:      { enable_annotations: true }
    templating:
        engines: ['twig']
        #assets_version: SomeVersionScheme
    default_locale:  "%locale%"
    trusted_hosts:   ~
    trusted_proxies: ~
    session:
        # handler_id set to null will use default session handler from php.ini
        handler_id:  ~
        save_path:   "%kernel.root_dir%/../var/sessions/%kernel.environment%"
    fragments:       ~
    http_method_override: true
    assets: ~

# Twig Configuration
twig:
    debug:            "%kernel.debug%"
    strict_variables: "%kernel.debug%"

# Doctrine Configuration
doctrine:
    dbal:
        driver:   pdo_mysql
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"
        charset:  UTF8
        # if using pdo_sqlite as your database driver:
        #   1. add the path in parameters.yml
        #     e.g. database_path: "%kernel.root_dir%/data/data.db3"
        #   2. Uncomment database_path in parameters.yml.dist
        #   3. Uncomment next line:
        #     path:     "%database_path%"

    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true

# Swiftmailer Configuration
swiftmailer:
    transport: "%mailer_transport%"
    host:      "%mailer_host%"
    username:  "%mailer_user%"
    password:  "%mailer_password%"
    spool:     { type: memory }

在参数下注意值app.locales: en|es|zh。现在这是我可以在创建路线时引用的值,如果我计划在将来支持更多的语言环境。对于那些好奇的人来说,那些路线是英语,西班牙语,中文。在注释中的DefaultController中,"%app.locales%"是引用config参数的部分。

我当前的方法的问题是/ admin例如没有将用户重定向到/ {browsers locale} / admin,这将是保持一切井井有条的更优雅的解决方案......但至少路线工作。仍在寻找更好的解决方案。

**** ****更新

我想我可能已经找到了答案,这是答案(Add locale and requirements to all routes - Symfony2),Athlan的回答。只是不确定如何在symfony 3中实现这一点,因为他的指示对我来说不够明确。

我认为这篇文章也可能有所帮助(http://symfony.com/doc/current/components/event_dispatcher/introduction.html

4 个答案:

答案 0 :(得分:9)

经过12个小时的调查,我终于找到了一个可以接受的解决方案。如果您可以提高效率,请发布此解决方案的修订版本。

有些事情要注意,我的解决方案特别适合我的需要。它的作用是强制任何URL转到本地化版本(如果存在)。

这需要在创建路径时遵循一些约定。

<强> DefaultController.php

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class DefaultController extends Controller
{

    /**
     * @Route("/{_locale}/", name="home_locale", requirements={"_locale" = "%app.locales%"})
     */
    public function indexAction(Request $request)
    {
        $translated = $this->get('translator')->trans('Symfony is great');

        // replace this example code with whatever you need
        return $this->render('default/index.html.twig', [
            'base_dir' => realpath($this->container->getParameter('kernel.root_dir').'/..'),
            'translated' => $translated
        ]);
    }

    /**
     * @Route("/{_locale}/admin", name="admin_locale", requirements={"_locale" = "%app.locales%"})
     */
    public function adminAction(Request $request)
    {
        $translated = $this->get('translator')->trans('Symfony is great');

        // replace this example code with whatever you need
        return $this->render('default/index.html.twig', [
            'base_dir' => realpath($this->container->getParameter('kernel.root_dir').'/..'),
            'translated' => $translated
        ]);
    }
}
?>

请注意,两条路线始终以&#34; / {_ locale} /&#34;开头。为此,项目中的每条路线都需要具备此功能。您之后只需输入真实的路线名称。对我来说,我对这种情况没问题。您可以轻松修改我的解决方案以满足您的需求。

第一步是在httpKernal上创建一个listen来拦截请求,然后再转发到路由器来呈现它们。

<强> LocaleRewriteListener.php

<?php
//src/AppBundle/EventListener/LocaleRewriteListener.php
namespace AppBundle\EventListener;

use Symfony\Component\HttpFoundation\RedirectResponse;

use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Routing\RouteCollection;

class LocaleRewriteListener implements EventSubscriberInterface
{
    /**
     * @var Symfony\Component\Routing\RouterInterface
     */
    private $router;

    /**
    * @var routeCollection \Symfony\Component\Routing\RouteCollection
    */
    private $routeCollection;

    /**
     * @var string
     */
    private $defaultLocale;

    /**
     * @var array
     */
    private $supportedLocales;

    /**
     * @var string
     */
    private $localeRouteParam;

    public function __construct(RouterInterface $router, $defaultLocale = 'en', array $supportedLocales = array('en'), $localeRouteParam = '_locale')
    {
        $this->router = $router;
        $this->routeCollection = $router->getRouteCollection();
        $this->defaultLocale = $defaultLocale;
        $this->supportedLocales = $supportedLocales;
        $this->localeRouteParam = $localeRouteParam;
    }

    public function isLocaleSupported($locale) 
    {
        return in_array($locale, $this->supportedLocales);
    }

    public function onKernelRequest(GetResponseEvent $event)
    {
        //GOAL:
        // Redirect all incoming requests to their /locale/route equivlent as long as the route will exists when we do so.
        // Do nothing if it already has /locale/ in the route to prevent redirect loops

        $request = $event->getRequest();
        $path = $request->getPathInfo();

        $route_exists = false; //by default assume route does not exist.

        foreach($this->routeCollection as $routeObject){
            $routePath = $routeObject->getPath();
            if($routePath == "/{_locale}".$path){
                $route_exists = true;
                break;
            }
        }

        //If the route does indeed exist then lets redirect there.
        if($route_exists == true){
            //Get the locale from the users browser.
            $locale = $request->getPreferredLanguage();

            //If no locale from browser or locale not in list of known locales supported then set to defaultLocale set in config.yml
            if($locale==""  || $this->isLocaleSupported($locale)==false){
                $locale = $request->getDefaultLocale();
            }

            $event->setResponse(new RedirectResponse("/".$locale.$path));
        }

        //Otherwise do nothing and continue on~
    }

    public static function getSubscribedEvents()
    {
        return array(
            // must be registered before the default Locale listener
            KernelEvents::REQUEST => array(array('onKernelRequest', 17)),
        );
    }
}

最后,设置services.yml以启动监听器。

<强> Services.yml

# Learn more about services, parameters and containers at
# http://symfony.com/doc/current/book/service_container.html
parameters:
#    parameter_name: value

services:
#    service_name:
#        class: AppBundle\Directory\ClassName
#        arguments: ["@another_service_name", "plain_value", "%parameter_name%"]
     appBundle.eventListeners.localeRewriteListener:
          class: AppBundle\EventListener\LocaleRewriteListener
          arguments: ["@router", "%kernel.default_locale%", "%locale_supported%"]
          tags:
            - { name: kernel.event_subscriber }

同样在config.yml中,您需要在参数下添加以下内容:

<强> config.yml

parameters:
    locale: en
    app.locales: en|es|zh
    locale_supported: ['en','es','zh']

我希望只有一个地方你定义了语言环境,但我最终不得不做2 ...但至少它们在同一个地方很容易改变。

app.locales用于默认控制器(requirements={"_locale" = "%app.locales%"}),locale_supported用于LocaleRewriteListener。如果它检测到列表中没有的语言环境,它将回退到默认语言环境,在本例中为locale的值:en。

app.locales与requirements命令相当不错,因为它会导致任何不匹配的语言环境有404.

如果您正在使用表单并登录,则需要对security.yml进行以下操作

<强> Security.yml

# To get started with security, check out the documentation:
# http://symfony.com/doc/current/book/security.html
security:
    encoders:
        Symfony\Component\Security\Core\User\User:
            algorithm: bcrypt
            cost: 12
        AppBundle\Entity\User:
            algorithm: bcrypt
            cost: 12

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

    providers:
        # http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
         database:
              entity: { class: AppBundle:User }
                #property: username
                # if you're using multiple entity managers
                # manager_name: customer

    firewalls:
        # disables authentication for assets and the profiler, adapt it according to your needs
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            pattern: ^/
            anonymous: true

            form_login:
                check_path: login_check
                login_path: login_route
                provider: database
                csrf_token_generator: security.csrf.token_manager

            remember_me:
                secret:   '%secret%'
                lifetime: 604800 # 1 week in seconds
                path:     /
                httponly: false
                #httponly false does make this vulnerable in XSS attack, but I will make sure that is not possible.
            logout:
                path:   /logout
                target: /

    access_control:
        # require ROLE_ADMIN for /admin*
        #- { path: ^/login, roles: ROLE_ADMIN }
        - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/(.*?)/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/, roles: ROLE_USER }

此处需要注意的重要更改是(.*?)/login将匿名进行身份验证,以便您的用户仍然可以登录。这确实意味着像.dogdoghere / login这样的路由可以触发,但是我将很快在登录路由上显示的要求会阻止这种情况并且会抛出404错误。我喜欢这个解决方案,(.*?)[a-z]{2},你想使用en_US类型的语言环境。

<强> SecurityController.php

<?php
// src/AppBundle/Controller/SecurityController.php
namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

class SecurityController extends Controller
{
    /**
     * @Route("{_locale}/login", name="login_route", defaults={"_locale"="en"}, requirements={"_locale" = "%app.locales%"})
     */
    public function loginAction(Request $request)
    {
        $authenticationUtils = $this->get('security.authentication_utils');

        // get the login error if there is one
        $error = $authenticationUtils->getLastAuthenticationError();

        // last username entered by the user
        $lastUsername = $authenticationUtils->getLastUsername();

        return $this->render(
            'security/login.html.twig',
            array(
                // last username entered by the user
                'last_username' => $lastUsername,
                'error'         => $error,
            )
        );
    }

    /**
     * @Route("/{_locale}/login_check", name="login_check", defaults={"_locale"="en"}, requirements={"_locale" = "%app.locales%"})
     */
    public function loginCheckAction()
    {
        // this controller will not be executed,
        // as the route is handled by the Security system
    }

    /**
    * @Route("/logout", name="logout")
    */
    public function logoutAction()
    {
    }
}
?>

请注意,即使这些路径前面也使用{_locale}。我喜欢这样,所以我可以为不同的语言环境提供自定义登录。要时刻铭记在心。唯一不需要语言环境的路由是logout工作得很好,因为它实际上只是安全系统的拦截路由。另请注意,它使用了config.yml中设置的要求,因此您只需在一个位置为项目中的所有路径编辑它。

希望这有助于某人尝试做我正在做的事情!

注意::为了便于测试,我使用了快速语言切换器&#39;谷歌浏览器的扩展程序,它会更改所有请求的接受语言标题。

答案 1 :(得分:8)

我没有足够的声誉来为正确的解决方案添加评论。所以我正在添加一个新答案

您可以在app / config / routing.yml中添加“prefix:/ {_ locale}”,如下所示:

app:
    resource: "@AppBundle/Controller/"
    type:     annotation
    prefix:   /{_locale}

因此,您无需将其添加到每个操作的每个路径。用于以下步骤。非常感谢你的完美。

答案 2 :(得分:2)

最终函数smallResumeOfResearching($ localeRewrite,$ opinion =&#39;恕我直言&#39;):)

  1. 该方法由先生提供。 Joseph使用/ {route_name}或/等路线工作得很好,但不使用像/ article / slug / other这样的路线。

  2. 如果我们使用由https://stackoverflow.com/a/37168304/9451542提供的修改后的mr.Joseph方法,我们将在开发模式下丢失探查器和调试器。

  3. 如果我们想要更灵活的解决方案,可以像这样修改onKernelRequest方法(感谢Joseph先生,感谢https://stackoverflow.com/a/37168304/9451542):

    public function onKernelRequest(GetResponseEvent $event)
    {
        $pathInfo = $event->getRequest()->getPathinfo();
        $baseUrl = $event->getRequest()->getBaseUrl();
        $checkLocale = explode('/', ltrim($pathInfo, '/'))[0];
    
        //Or some other logic to detect/provide locale
    
        if (($this->isLocaleSupported($checkLocale) == false) && ($this->defaultLocale !== $checkLocale)) {
            if ($this->isProfilerRoute($checkLocale) == false) {
                $locale = $this->defaultLocale;
                $event->setResponse(new RedirectResponse($baseUrl . '/' . $locale . $pathInfo));
        }
        /* Or with matcher:
        try {
             //Try to match the path with the locale prefix
             $this->matcher->match('/' . $locale . $pathInfo);
             //$event->setResponse(new RedirectResponse($baseUrl . '/' . $locale . $pathInfo));
        } catch (\Symfony\Component\Routing\Exception\ResourceNotFoundException $e) {
        } catch (\Symfony\Component\Routing\Exception\MethodNotAllowedException $e) {
        }
        */
        }
    }
    

    注意:$ this-&gt; profilerRoutes = array(&#39; _profiler&#39;,&#39; _wdt&#39;,&#39; _error&#39;);

  4. 感谢Susana Santos指向简单的配置方法:)

答案 3 :(得分:1)

Symfony 3.4的小改进:

  1. 请确保getSubscribedEvents()将在RouterListener :: onKernelRequest和BEFORE LocaleListener :: onKernelRequest之前注册LocaleRewriteListener。整数17必须大于RouterListener :: onKernelRequest priotity。否则你将得到404。

    bin / console debug:event-dispatcher

  2. services.yml中的服务定义必须是(取决于Symfony配置):

    的appbundle \事件监听\ LocaleRewriteListener:     参数:['@router','%kernel.default_locale%','%locale_supported%']     标签:          - {name:kernel.event_subscriber,event:kernel.request}