Apr 03
How to get the controller’s action name from the view
You know sometimes, in CakePHP’s views, it is useful to check what is the controller’s action. It is quite useful when it comes to implementing selected tabs.
You can use the following expression to get the action name from your view:
$this->controller->action
The following is a usage example:
<?php /* This is a view element file – nav_messages.thtml */ ?>
<?php $s = $this->controller->action; ?>
<ul class="generic_subtabs">
<li class="<?php echo ($s==‘messages_inbox’)?‘selected’:” ?>" >
<a href="#" title="Inbox">Inbox</a>
</li>
<li class="<?php echo ($s==‘messages_sentbox’)?‘selected’:” ?>">
<a href="#" title="Sentbox">Sentbox</a>
</li>
</ul>
<?php $s = $this->controller->action; ?>
<ul class="generic_subtabs">
<li class="<?php echo ($s==‘messages_inbox’)?‘selected’:” ?>" >
<a href="#" title="Inbox">Inbox</a>
</li>
<li class="<?php echo ($s==‘messages_sentbox’)?‘selected’:” ?>">
<a href="#" title="Sentbox">Sentbox</a>
</li>
</ul>
“messages_inbox” and “messages_sentbox” is the name of the action (function) in my controller.
This way you can get the list tabs to be selected visually. There is a class named “selected” that is defined in the CSS. I tested this on CakePHP 1.1. The code works if it’s in an element too.
I just realized my code font size is too huge.
Possibly related: