ganeshran
ganeshran

Reputation: 3512

CakePHP - passing constructor arguments to custom components

I am trying to use a custom class in cakephp. Initially I had created a vendor class which works fine but I can't use other cakephp components.

To use built in components like $this->Text, I can create a custom component but the constructor requires a argument which is a json object returned from an API and I need to keep initializing in a loop

//The constructor for the class
function __construct($objValue) {
$this->messageId = $objValue['id'];

Is using a component suitable for this purpose?

Upvotes: 0

Views: 952

Answers (1)

mark
mark

Reputation: 21743

you don't need to create a component here if you don't need it in the controller scope. also you don't need to make it a vendor class (which is third party stuff).

cake offers you a way out: Libs in APP/Lib You can use them anywhere anytime.

App::uses('MyClassName', 'Lib');
$MyClass = new MyClassName();

You might even want to create a package in Lib itself - e.g. "Lib/Utility":

App::uses('MyClassName', 'Utility');

without knowing any more about what exactly this custom class does, it is difficult to be any more specific here.

Upvotes: 1

Related Questions