我需要创建用户友好的 URL,这些 URL 已翻译,但指向与其他语言相同的 Controller 。
例子:
/en/myCar/100 ->/de/meinAuto/100 ->/fr/monVoiture/100
我尝试使用路由,但找不到根据翻译后的 URL 调用 Controller 的方法。
我应该从哪里开始的任何提示?
最佳答案
使用内置功能无法轻松完成,但有人有同样的需求并专门为此编写了一个包:yii2-localeurls .我想这可能就是您要找的:
With this extension you can use URLs that contain a language code like:
/en/some/page
/de/some/page
http://www.example.com/en/some/page
http://www.example.com/de/some/pageYou can also configure friendly names if you want:
http://www.example.com/english/some/page
http://www.example.com/deutsch/some/pageThe language code is automatically added whenever you create a URL, and read back when a URL is parsed. For best user experience the language is autodetected from the browser settings, if no language is used in the URL. The user can still access other languages, though, simply by calling a URL with another language code.
The last requested language is also persisted in the user session and in a cookie. So if the user tries to access your site without a language code in the URL, he'll get redirected to the language he had used on his last visit.
关于routes - yii2 创建翻译后的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30620404/