Access Methods From Another Controller in CakePHP3

Access Methods From Another Controller in CakePHP3

Sometimes you need to access a method from another controller.  This can help (or reduce) rigidity within the CakePHP framework, in that logic specific to one controller can still be contained within that controller, but then be referenced from another controller.

This is a very simple process.  In the example below, we have a controller named CIMProfile. We want to access the getCIMCustomerProfile in that controller, from another controller.

In your controller method, place the following code referencing the source controller, where the other method resides:

$oCimProfile = new CIMProfile();
$response_body = $oCimProfile->getCIMCustomerProfile($this->Auth->user('anet_cim_profile_id'));      

 

Note, in the source controller, your method must be public, for example:

class CIMProfile() {
   ....

   public function getCIMCustomerProfile(profile_id) {

    ...

   }
}

 

Share this Post