• Archives

  • Categories

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>

“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:

  1. CakePHP’s built-in Pages Controller
  2. How to perform routing in CakePHP 1.2
  3. How to do logging in CakePHP
  4. CakePHP 1.2 and PHP 5.3 doesn’t play nice

No comments yet. Leave yours here.


WordPress powered and Django inspired.
Love and elephants come after.
RSS: Posts and comments.