Reputation: 100
When I have a function which needs to be executed on every page and assigns a variable to the default layout, where should I add the function?
I was thinking about the AppController where I add a private function and call this private function in the beforeFilter function where I then assign it to a view variable, but is this the right way to do this?
Small example of what I mean:
<?php
App::uses('Controller', 'Controller');
class AppController extends Controller {
public function beforeFilter(){
$this->set('something', $this->someFunction());
}
private function someFunction(){
return 'something';
}
}
?>
Upvotes: 1
Views: 49
Reputation: 25698
You're doing it right, your example looks good. beforeFilter() or beforeRender() is a good place for that. Just don't forget to cache your data except it changes on each page.
Upvotes: 1