How to identify active menu link in CakePHP

前端 未结 5 529
南笙
南笙 2021-01-11 22:22

I\'m creating an accordion layout for an admin sidebar. Now I need to identify the active link and add a class active to that link. Here is my code:

<         


        
5条回答
  •  情话喂你
    2021-01-11 23:16

    I know this is pretty old but i found a good solution.

    Based on Faisal's answer i wrote my own simple Helper:

    App::uses('AppHelper', 'View/AppHelper');
    
    class PVHtmlHelper extends AppHelper {
    
        public $helpers = array('Html');
    
        public function link($title = null, $url = null, $options) {
    
            if ($title == null || $url == null)
                return;
    
            $class = (($this->params['controller']===$url['controller']) && ($this->params['action']==$url['action']) )?'active' :'';
    
            echo "
  • " . $this->Html->link($title, $url, $options) . "
  • "; } }

    Maybe you need to modify the echo

  • inside the function to fit your needs.

    Example:

    echo $this->PVHtml->link('Login', array('controller' => 'users', 'action' => 'login'));
    

提交回复
热议问题