使用 HTML-Helper 在 CakePHP 中路由子域

发布于 2024-11-03 09:43:34 字数 564 浏览 0 评论 0原文

我有一个在子域“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无人接听 2024-11-10 09:43:34

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

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

/config/ 中paths.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/ 访问该站点Tips 或 www.example.com/mobile/tips

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

有关 CakePHP-1.3 中更高级的路由,请参阅 Mark Story 有关自定义路线类的文章

让我们知道;)

Gathering code from the page you mentioned:

Constraint: you cannot have a controller called tips or foo in this setup

In /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'));

In your Controller action:

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

Then in your view:

<?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');
}

?>

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 that m.example.com and www.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:

<?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);

?>

And in your view you don't need to worry about checking whether the site is being accessed via m.example.com/tips or www.example.com/mobile/tips:

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

For more advanced routing in CakePHP-1.3 refer to Mark Story's article on custom Route classes

Let us know ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文