Answer the question
In order to leave comments, you need to log in
How do I know if the requested action is in the controller?
The site has a main menu. There are links in the menu. Everything seems to be simple, because everyone has it.
To highlight the active menu item, a check is used:
$liCssClass = Yii::$app->controller->id == 'news' ? 'menu-item-active' : 'menu-item';
<li class="<?php echo $liCssClass ?>">Новости</li>
$currentController = \Yii::$app->controller;
$currentAction = $currentController->action;
foreach($links as $link){
list($controller, $actionId) = \Yii::$app->createController($link->url);
if ($controller && $actionId) {
if ($currentController->id == $controller && $actionId == $currentAction->id){
$cssClass = 'menu-item-active';
}
else {
$cssClass = 'menu-item';
}
$ul[] = Html::tag('li', $link->title, ['class' => $cssClass]);
}
}
$controller
can return false, or $actionId
it can be absent in $controller
(after all, links are taken from the database, and there you can make a mistake and insert a broken link). $controller->hasMethod('action'.$actionId)
, or is it shit code?
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question