以下是在 stackoverflow 上 查到的 希望对你有帮助
if you have some advanced link structure, put method below in Controller
/**
* Checks if the current route matches with given routes
* @param array $routes
* @return bool
*/
public function isActive($routes = array())
{
$routeCurrent = '';
if ($this->module !== null) {
$routeCurrent .= sprintf('%s/', $this->module->id);
}
$routeCurrent .= sprintf('%s/%s', $this->id, $this->action->id);
foreach ($routes as $route) {
$pattern = sprintf('~%s~', preg_quote($route));
if (preg_match($pattern, $routeCurrent)) {
return true;
}
}
return false;
}
//usage
'items'=>array(
array('label'=>'Some Label', 'url'=>array('some/route'),'active'=>$this->isActive(array(
'some/route',
'another/route',
)),
),
使用融合bootstrap的yii 就无需上面这种形式了。