繁体   English   中英

检查cakephp上的路由URL

[英]check route url on cakephp

大家好,我在cakephp 2网站上有一个像这样的菜单:

<ul class="nav">
<li><?php echo $this->Html->link('Home', array('controller' => 'posts', 'action' => 'index')); ?></li>
<li><?php echo $this->Html->link('Add post', array('controller' => 'posts', 'action' => 'add')); ?></li>
<li><a href="#contact">Contact</a></li>
</ul>

而且我必须检查我是否在页面上以在菜单链接上添加class =“ selected”。 我怎样才能做到这一点?

谢谢

在您的视图文件中,您还可以执行以下操作:

$this->request->params

我建议您编写自己的助手,该助手将实现与HtmlHelper :: link具有相同args的方法,并在内部调用并返回HtmlHelper,但在此之前,它将$ this-> request-> params与传递的第一个arg数组进行比较。 如果匹配,则可以在第三个参数中插入类名称。

这样的事情,可以自己实现:

class MyHelper extends AppHelper {
    public $helpers = array('Html');
    public function link($title, $url, $options) {
    /** 
     * if ($this->View->request->params ...
     * do your matching logic here
     * and if it matches: $options['class'] = 'active';
     */
    return $this->Html->link($title, $url, $options
}

我写了一段时间的(CakePHP 1.2)帮助程序,它会自动执行此操作:

http://richardathome.com/blog/cakephp-smarter-links

应该很直接地将其移植到2.0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM