在 CakePHP 中为每个 URL 添加前缀 [英] Adding a prefix to every URL in CakePHP

查看:19
本文介绍了在 CakePHP 中为每个 URL 添加前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 CakePHP 中为每个 URL 添加前缀(例如语言参数)的最简洁方法是什么?

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/控制器/动作.

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.

所以问题是双重的:

  • 构建路由的最佳方式是什么,以便默认情况下隐式包含语言参数,而不必为每个新定义的路由指定?
    • 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 in AppController::beforeFilter().
      • Router::url(array('controller' => 'foo', 'action' => 'bar')) 应该返回 /en/foo/bar
      • 由于Controller::redirect()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 even Router::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
      • JsHelper::load_
      • JsHelper::redirect_
      • View::uuid,但仅用于哈希生成
      • Controller::redirect
      • Controller::flash
      • Dispatcher::__extractParams via Object::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 in AppController or maybe just in bootstrap.php to handle the parameter insertion. The overridden Controller and Helper methods would use this function, as would I if I wanted to manually call Router::url. Would this be sufficient?

      推荐答案

      来自 IRC 的 rchavik 建议此链接:基于CakePHP URL 的i18n 和l10n 国际化和本地化语言切换

      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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆