Retrieve the Name of a Controller and Action Being Called In A Cake View

February 13th, 2008 - cake php

There are times when you may want your layout in Cake to perform slightly differently when you are viewing pages that call a certain controller or even a specific action in that controller. This is common if you have javascript that you only want to include on a single page of your application or maybe render an element only at certain times.

The following variables can be called from any view in Cake.

To retrieve the name of the controller that is currently being called, you can use:

Code

To retrieve the name of the action that is currently being called, you can use:

Code

Using these attributes above with logical operators in your layouts will allow you to create a single layout file that can accommodate the needs of all of your controllers and actions, rather than creating several different layouts.

Questions / Comments / Suggestions

You can reach me through the contact page if you have any comments, corrections, or suggestions on this article.