向CakePHP中的每个URL添加前缀 [英] Adding a prefix to every URL in CakePHP
问题描述
为CakePHP中的每个网址添加前缀的最干净的方法是什么,像语言参数?
What's the cleanest way to add a prefix to every URL in CakePHP, like a language parameter?
http://example.com/en/controller/action
http://example.com/ru/admin/controller/action
它需要使用 admin
的真实前缀,理想的是裸URL / controller / action
可以重定向到 / DEFAULT-LANGUAGE / controller / action
。
It needs to work with "real" prefixes like admin
, and ideally the bare URL /controller/action
could be redirected to /DEFAULT-LANGUAGE/controller/action
.
为我提供了适合的应用程序,但它是一种黑客,我需要在大多数链接手动包含语言参数,这是不好的。
It's working in a retro-fitted application for me now, but it was kind of a hack, and I need to include the language parameter by hand in most links, which is not good.
该问题有两个:
- 结构路由的最佳方法是什么,因此语言参数默认是隐式包含的,每个新定义的Route?
-
Router :: connect('/:controller /:action / *',...)
- 该参数应该在
$ this-> params ['lang']
中可用AppController :: beforeFilter()
。
- What's the best way to structure Routes, so the language parameter is implicitly included by default without having to be specified for each newly defined Route?
Router::connect('/:controller/:action/*', ...)
should implicitly include the prefix.- The parameter should be available in
$this->params['lang']
or somewhere similar to be evaluated inAppController::beforeFilter()
.
-
Router :: url(array('controller'=>'foo','action'=>'bar')) code>应返回
/ en / foo / bar
- ,
Form :: create()
或甚至Router :: url()
具有相同的行为,覆盖每个单一的功能不是真的一个选项。Html :: image()
例如应该产生一个无前缀的URL。
Router::url(array('controller' => 'foo', 'action' => 'bar'))
should return/en/foo/bar
- Since
Controller::redirect()
,Form::create()
or evenRouter::url()
directly need to have the same behavior, overriding every single function is not really an option.Html::image()
for instance should produce a prefix-less URL though.
以下方法似乎调用
Router :: url
。-
Controller :: redirect
-
Controller :: flash
-
Dispatcher :: __ extractParams
Object :: requestAction
-
Helper :: url
li>
-
JsHelper :: load _
-
JsHelper :: redirect _
-
View :: uuid
,但仅适用于哈希生成
Controller::redirect
Controller::flash
Dispatcher::__extractParams
viaObject::requestAction
Helper::url
JsHelper::load_
JsHelper::redirect_
View::uuid
, but only for a hash generation
除了这些,似乎Controller和Helper方法需要重写,我可以没有
JsHelper
。我的想法是在AppController
中编写一个通用函数,或者在bootstrap.php
中处理参数插入。重写的Controller和Helper方法将使用这个函数,如果我想手动调用Router :: url
。这是足够了吗?Out of those it seems the Controller and Helper methods would need to be overridden, I could live without the
JsHelper
. My idea would be to write a general function inAppController
or maybe just inbootstrap.php
to handle the parameter insertion. The overridden Controller and Helper methods would use this function, as would I if I wanted to manually callRouter::url
. Would this be sufficient?推荐答案
rchavik from IRC suggested this link: CakePHP URL based language switching for i18n and l10n internationalization and localization
一般来说,似乎重写
Helper :: url
可能是解决方案。In general, it seems that overriding
Helper::url
might be the solution.这篇关于向CakePHP中的每个URL添加前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-