Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Gathering code from the page you mentioned:</p> <p><strong>Constraint</strong>: you cannot have a controller called <code>tips</code> or <code>foo</code> in this setup</p> <p>In <code>/config/routes.php</code>:</p> <pre><code>$subdomain = substr( env("HTTP_HOST"), 0, strpos(env("HTTP_HOST"), ".") ); if( strlen($subdomain)&gt;0 &amp;&amp; $subdomain != "m" ) { Router::connect('/tips',array('controller'=&gt;'mobiles','action'=&gt;'tips')); Router::connect('/foo', array('controller'=&gt;'mobiles','action'=&gt;'foo')); Configure::write('Site.type', 'mobile'); } /* The following is available via default routes '/{:controller}/{:action}'*/ // Router::connect('/mobiles/tips', // array('controller' =&gt; 'mobiles', 'action'=&gt;'tips')); // Router::connect('/mobiles/foo', // array('controller' =&gt; 'mobiles', 'action'=&gt;'foo')); </code></pre> <p>In your Controller action:</p> <pre><code>$site_is_mobile = Configure::read('Site.type') ?: ''; </code></pre> <p>Then in your view:</p> <pre><code>&lt;?php if ( $site_is_mobile ) { // $html will take care of the 'm.example.com' part $html-&gt;link('Cool Tips', '/tips'); $html-&gt;link('Hot Foo', '/foo'); } else { // $html will just output 'www.example.com' in this case $html-&gt;link('Cool Tips', '/mobiles/tips'); $html-&gt;link('Hot Foo', '/mobiles/foo'); } ?&gt; </code></pre> <p>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 <code>$html</code> helper will not be able -- by no amount of magic -- to use controller-action routes to another domain. Be aware that <code>m.example.com</code> and <code>www.example.com</code> are different domains as far as the <code>$html</code> helper is concerned.</p> <p>Now, if you want you can do the following in your controller to take some logic off your view:</p> <pre><code>&lt;?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-&gt;set($tips_url); $this-&gt;set($foo_url); ?&gt; </code></pre> <p>And in your view you don't need to worry about checking whether the site is being accessed via <code>m.example.com/tips</code> or <code>www.example.com/mobile/tips</code>:</p> <pre><code>&lt;?php echo $html-&gt;link("Get some kewl tips", $tips_url); ?&gt; </code></pre> <p>For more advanced routing in CakePHP-1.3 refer to <a href="http://mark-story.com/posts/view/using-custom-route-classes-in-cakephp" rel="nofollow">Mark Story's article on custom <code>Route classes</code></a></p> <p>Let us know ;)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload