【问题标题】:Routing a subdomain in CakePHP with HTML-Helper使用 HTML-Helper 在 CakePHP 中路由子域
【发布时间】:2026-01-08 21:20:04
【问题描述】:

我有一个在子域“m.mydomain.com”上运行的移动页面。这一切都很好,但是我想在使用子域时删除 URL 中的控制器。

m.mydomain.com/mobiles/tips

应该变成

m.mydomain.com/tips

通过使用 HTML 助手。

目前的链接如下所示:

$html->link('MyLink', array('controller' => 'mobiles', 'action'=> 'tips'));

我尝试了几种可能的路由解决方案,以及引导程序中的一些技巧,但对我来说并不奏效。

在 CakeBakery 中我找到了 this,但这并没有解决我的问题。

有人对这个问题有想法吗?

【问题讨论】:

  • 每当您看到我的答案时,如果它不起作用,请发表评论,以便我们努力寻找明确的答案。这个问题可能对其他人有帮助。

标签: php cakephp mobile cakephp-1.3 html-helper


【解决方案1】:

从您提到的页面收集代码:

约束:您不能在此设置中使用名为 tipsfoo 的控制器

/config/routes.php:

$subdomain = substr( env("HTTP_HOST"), 0, strpos(env("HTTP_HOST"), ".") );

if( strlen($subdomain)>0 && $subdomain != "m" ) { 
    Router::connect('/tips',array('controller'=>'mobiles','action'=>'tips'));
    Router::connect('/foo', array('controller'=>'mobiles','action'=>'foo'));
    Configure::write('Site.type', 'mobile');
}

/* The following is available via default routes '/{:controller}/{:action}'*/
// Router::connect('/mobiles/tips', 
//                  array('controller' => 'mobiles', 'action'=>'tips'));
// Router::connect('/mobiles/foo',  
//                  array('controller' => 'mobiles', 'action'=>'foo'));

在您的控制器操作中:

$site_is_mobile = Configure::read('Site.type') ?: '';

那么在你看来:

<?php

if ( $site_is_mobile ) {
    // $html will take care of the 'm.example.com' part
    $html->link('Cool Tips', '/tips');
    $html->link('Hot Foo', '/foo');
} else {
    // $html will just output 'www.example.com' in this case
    $html->link('Cool Tips', '/mobiles/tips');
    $html->link('Hot Foo', '/mobiles/foo');
}

?>

这将允许您在视图中输出正确的链接(稍后我将向您展示如何编写更少的代码)但 $html 助手将无法 - 无论如何魔法 -使用控制器动作路由到另一个域。请注意,就 $html 帮助器而言,m.example.comwww.example.com 是不同的域。

现在,如果您愿意,您可以在控制器中执行以下操作,以消除视图中的一些逻辑:

<?php

$site_is_mobile = Configure::read('Site.type') ?: '';

if ( $site_is_mobile !== '' ) {
    $tips_url = '/tips';
    $foo_url  = '/foo';
} else {
    $tips_url = '/mobile/tips';
    $foo_url  = '/mobile/foo';
}

// make "urls" available to the View
$this->set($tips_url);
$this->set($foo_url);

?>

在您看来,您无需担心是否通过m.example.com/tipswww.example.com/mobile/tips 访问该站点:

<?php echo $html->link("Get some kewl tips", $tips_url); ?>

CakePHP-1.3 中更高级的路由参考Mark Story's article on custom Route classes

让我们知道 ;)

【讨论】:

  • 感谢您的帮助 Oerd!这行得通,但我一直在寻找可以双向工作的东西:www.mydomain.com/mobiles/tips 和 m.mydomain.com/tips。使用 html 助手可以做到这一点吗?
  • @chris 如果你有一个手机控制器,它可以开箱即用,前提是 your_controller 在现实中被调用 mobiles_controller.php
  • @chris,我正在更新答案
  • @Oerd 再次感谢。所以我需要有 If/Else 语句?路由与 html 助手相结合是不可能为我发挥魔力的,只是为了使用它: $html->link('MyLink', array('controller' => 'mobiles', 'action'= > '提示'));
  • @Oerd 即使这不是我想要的,我也会选择这个解决方案,因为我没有其他/更好的想法:-) 再次感谢!我认为routes.php可能有一个解决方案,因为这里有魔法,例如Router::connect('/test', array('controller' =&gt; 'mobiles', 'action' =&gt; 'index'));$html-&gt;link('MyLink', array('controller' =&gt; 'mobiles', 'action'=&gt; 'index')); 将创建此链接 /test 而不是 /mobiles。所以我想我可以使用路由以某种方式删除/mobiles...但是无论如何,我现在会接受你的建议:-)