Cakephp

Use a function from one controller in another controller

Use a function from one controller in another controller


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;
$calledcontroller->functionIwantToAccess();

Published: 5th December 2016 by

Adverts