Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cakePHP - how do I conditionally load a component into a controller?

Tags:

cakephp

I'm using the great Facebook plugin for cakephp 1.3 by http://www.webtechnick.com. This is what I have at the moment:

class UsersController extends AppController {
    var $name = 'Users';
    var $components = array('Facebook.Connect');

    function beforeFilter {
        $this->set('facebookUser', $this->Connect->user());
    }
}

But I want to load the Facebook.Connect component conditionally, and use it in the controller - something like this in sudocode...

if ($thisIsTrue) {
    Load_the_component_and_make_it_ready_for_use;
    $this->set('facebookUser', $this->Connect->user());
}

How should I do this?

like image 720
Owen Avatar asked Dec 10 '22 14:12

Owen


1 Answers

If you are doing this in CakePHP 2.0 it is really easy. I found this SO thread which discouraged me originally, but it must be a 1.3 and lower issue. For example:

public function beforeFilter(){
    parent::beforeFilter();
    $this->Paypal = $this->Components->load('Paypal');
}

And that's all she wrote.

like image 200
bitwit Avatar answered May 30 '23 04:05

bitwit