检查 CakePHP 上的路由网址


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 具有相同参数的方法,并在内部调用并返回 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 应该非常简单