Login Register


Use a function from one controller in another controller

There is two ways to re-use functionality in CakePHP. One way it by place your code in a component and then reference the component. This is probably more correct, but I find the component does not have access to the Cakephp elements like sessions, save(), find() etc. So to keep it simple I prefer to place the functionality I need in a controller and reference the function in the controller with $this->myfunction(); then I can also use it in another controller like this

App::import('Controller', 'Nameofcontroller');
$calledcontroller = new NameofcontrollerController;

Posted on: 2016-12-05 Please login/register to leave comments