开发者

Link for static page in CakePHP without appending the controller

So I created a couple of static pages in views > pages folder. They are contact.ctp and privacy.ctp. In my routes.php, I made it so that they could be viewed by going to domain.com/contact and domain.com/privacy with:

开发者_如何学Go
    Router::connect('/contact', array('controller' => 'pages', 'action' => 'display', 'contact'));

    Router::connect('/privacy', array('controller' => 'pages', 'action' => 'display', 'privacy'));

Now, when I link them at the footer with:

    <li><?= $this->Html->link('Contact', array('controller' => 'pages', 'action' => 'display', 'contact')); ?></a></li>

    <li><?= $this->Html->link('Privacy', array('controller' => 'pages', 'action' => 'display', 'privacy')); ?></a></li>

They are linked as domain.com/pages/terms. How can I stop it from appending the pages controller without giving an absolute url (i.e. without doing: <?= $this->Html->link('Contact', 'http://www.domain.com/contact'); ?> or is that the only other way?


you probably put these routes after Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display')); Just reverse that order and it should work.


ROUTE

Router::connect('/*', array('controller' => 'pages', 'action' => 'display'));

VIEW

 echo $this->Html->link('Target', $this->Html->url(array('controller'=>'pages', 'action'=>'display', 'target', 'ext'=>'html')));

OUPUT

<a href="target.html">Target</a>


Use an actual link?

<a href ="/contact">Contact</a>

And:

<a href ="/privacy">Privacy</a>

Short and sweet ^_^


For SE posterity, and the sake of succinctness, you can use Router::url( ).

<li><a href="<?php echo Router::url('/contact'); ?>">Contact</a></li>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜