Reputation: 1961
I am trying to include a small code in each page of my site. Is there any way to do this without modifying each controller?
For example - I want to read/unread message from Message
model.
Can i do this using the app_controller? I have add following function in app_controller.php. I need suggestion. Please help me.
function messageStatus() {
App::import('Model','Message');
$new_message = $this->Message->find(
'first',
array (
'conditions' => array (
'Message.status' => '1'
)
)
);
$this->set("new_message",$new_message);
}
Upvotes: 0
Views: 45
Reputation: 46
As user559744 mentioned you can use AppController within your application to create attributes and methods that can be accessed by your controllers. AppController is the parent class of your controllers.
You should copy app_controller.php from /cake/libs/controller/ to YOURAPP/app_controller.php to avoid making changes to the core files.
http://book.cakephp.org/view/957/The-App-Controller
Upvotes: 0
Reputation: 13712
Depending on when you want to execute your actions, you will have to override in the app_controller.php
file one of the following functions (according to the documentation), :
beforeFilter()
afterFilter()
beforeRender()
Since all your other controllers will be inheriting the methods of this class, your actions will be executed every time (as specified in the docs) one of your controllers are executed.
If you want to have a controller that does not run the code in the app_controller
simply override the method again locally.
Upvotes: 1