問題描述
翻譯 symfony2 路由錯誤 (translation symfony2 routing error)
The error if access a /login
is:
No route found for "GET /login"
when I try to access /en/login
redirects me to login but I get the same error.
In my routing.yml I have:
<pre class="lang‑yml prettyprint‑override">login: pattern: /{_locale}/login defaults: { _controller: miomioBundle:Security:login} requirements: _locale: es|en|de options: expose: true
</pre>
‑‑‑‑‑
參考解法
方法 1:
This sounds like you have two separate issues.
The route /login does not exist. You have setup three routes above /es/login, /en/login and /de/login but not /login on it's own.
You must have some access controls setup that do not allow the three routes above to be access be unauthenticated users, hence you are being redirected to /login to authenticate... but as mentioned in point 1 that route does not exist.
To solve point 1 you actually need to setup a route which handles the form for login. See the Symfony docs for step by step approach: http://symfony.com/doc/current/book/security.html#using‑a‑traditional‑login‑form
For point 2 you will need to setup some access controls in your security.yml file to allow users to access your login pages without being logged in:
access_control:
‑ { path: ^/es/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
‑ { path: ^/en/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
‑ { path: ^/de/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
You must already have an access control like the below otherwise your login page would crash in an infinite loop:
access_control:
‑ { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
I have to say though, if it just a simple login form then you would be much better served by sticking with 1 login form and handling the labels and login text with translations instead. http://symfony.com/doc/master/book/translation.html
方法 2:
Take a look at the JMSTranslation Bundle and something that may be of more interest for you the JMSI18nRoutingBundle. The JMSI18nRoutingBundle allows you to specify how your routing will work within your config.yml file.
If you want your URL to be /login using the default locale you'll set something like this up:
jms_i18n_routing:
default_locale: en
locales: [de, en]
strategy: prefix_except_default
However if you want your url to always contain a locale, for example: en/login or de/login you'll want to change the stratagy parameter to 'prefix'
jms_i18n_routing:
default_locale: en
locales: [en, de]
strategy: prefix