mikemack
mikemack

Reputation: 36

Adding component in cakephp 2.3.4

How to add a component with check first if the component is exist. I used this on cakephp 2.2.3

public function __construct( $request = null, $response = null ) {
    parent::__construct( $request, $response );
    $this->_setupApplicationComponents();
}

protected function _setupApplicationComponents() {
if ( App::import( 'Component', 'Search.Prg' ) ) {
    $this->components[] = 'Search.Prg';
}
}

it not work on cakephp 2.3.4.
Anyone can help.
Thanks

Upvotes: 0

Views: 885

Answers (1)

thaJeztah
thaJeztah

Reputation: 28987

I'm not really sure why you would check if a component exists before using it; CakePHP will automatically produce errors if a component could not be found?

CakePHP 2.3 uses 'lazy loading', which means that the component is not actually loaded/constructed until it is actually used. This means a lot less overhead, and will make your application work faster.

To indicate that you may use a certain class (component), use App::uses(); See Loading Classes

In your situation, to load the Prg component from the Search Plugin;

App::uses('Prg', 'Search.Controller/Component');

However, to use a component, just add it to the $components array of your Controller, and CakePHP should handle it automatically;

public $components = array(
    // Pluginname.Componentname
    'Search.Prg',
);

See Using Components

Upvotes: 1

Related Questions