使用 HTML-Helper 在 CakePHP 中路由子域
我有一个在子域“m.mydomain.com”上运行的移动页面。这一切工作正常,但我想在使用子域时删除 URL 中的控制器。
m.mydomain.com/mobiles/tips
应该
m.mydomain.com/tips
通过使用 HTML-Helper 来实现。
目前,链接看起来像这样:
$html->link('MyLink', array('controller' => 'mobiles', 'action'=> 'tips'));
我尝试了几种可能的路线解决方案以及引导程序中的一些技巧,但对我来说没有成功。
在 CakeBakery 中,我发现 这个 但这确实不能解决我的问题。
有人对这个问题有想法吗?
I have a mobile page running on a subdomain "m.mydomain.com". This is all working fine, but I would like to remove the controller in the URL when using the subdomain.
m.mydomain.com/mobiles/tips
should become
m.mydomain.com/tips
by using the HTML-Helper.
At the moment a link looks like that:
$html->link('MyLink', array('controller' => 'mobiles', 'action'=> 'tips'));
I tried several possible solutions with the routes and also some hacks in the bootstrap but it did not work out for me.
In the CakeBakery I found this but that does not solve my issue.
Does anyone have an idea for this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您提到的页面收集代码:
约束:在此设置中,您不能有名为
tips
或foo
的控制器在
/config/ 中paths.php
:在您的控制器操作中:
然后在您的视图中:
这将允许您在视图中输出正确的链接(稍后我将向您展示如何编写更少的代码)但是
$html
助手将无法 -- 通过没有任何魔法——使用控制器操作路由到另一个域。请注意,对于$html
帮助程序而言,m.example.com
和www.example.com
是不同的域。现在,如果您愿意,可以在控制器中执行以下操作,以从您的视图中删除一些逻辑:
并且在您的视图中,您无需担心检查是否正在通过 m.example.com/ 访问该站点Tips 或
www.example.com/mobile/tips
:有关 CakePHP-1.3 中更高级的路由,请参阅 Mark Story 有关自定义
路线类
的文章让我们知道;)
Gathering code from the page you mentioned:
Constraint: you cannot have a controller called
tips
orfoo
in this setupIn
/config/routes.php
:In your Controller action:
Then in your view:
This will allow you to output the right links in your views (in a bit I'll show you how to write even less code) but the
$html
helper will not be able -- by no amount of magic -- to use controller-action routes to another domain. Be aware thatm.example.com
andwww.example.com
are different domains as far as the$html
helper is concerned.Now, if you want you can do the following in your controller to take some logic off your view:
And in your view you don't need to worry about checking whether the site is being accessed via
m.example.com/tips
orwww.example.com/mobile/tips
:For more advanced routing in CakePHP-1.3 refer to Mark Story's article on custom
Route classes
Let us know ;)