tags:

views:

43

answers:

2

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?

+1  A: 

Since the Component is initialized while loading with the controller I wouldn't recommend loading it later on.

Like maggie commented you could load the component ( http://book.cakephp.org/view/939/Loading-Components ) but then you'd have to call startup and initialize yourself and attach the object to your controller.

All in all it might be easier to just make the $this->set... conditional and let the component load every time.

stefan
The problem with letting the component load every time is that, if the user is logged in Facebook I can't log them out of my site because the component syncs facebook with the Auth component and logs the user back in. One answer is to log the user out of facebook first, but that's not the behaviour I want. I could mess with the component but as it is maintained so I don't really want to do that. So I'll try and conditionally attach the object myself. Cheers.
Mark Flint
Ah, the initialize method is defined inside the component - so I'm messing with the component after all...
Mark Flint
So, it turns out that this Facebook.Connect component already has an initialize method. Instead of conditionally attaching the component, I modified the initialize method to conditionally sync the user with Auth (thereby loggin in to site) based on the state of a session var I set on a manual logout from the site.
Mark Flint
+1  A: 

Unless there's something I'm missing here, you use App::import to import a component.

I've heard you don't want to do this with models, because there are other things setup behind the scenes. But components should be fine.

if( $condition ) {
 App::import( 'Component', 'MyComponent' );
 $this->MyComponent = new MyComponent();
 $this->MyComponent->method();
}

HTH, Travis

Travis Leleu
I tried the above method. The complication is that this component makes heavy use an initialize() callback that (I think) does stuff before the controller loads. So before the users controller loads, the component logs into Facebook, and then in the beforeFilter of your controller you can then set the viewVars with user info.
Mark Flint
Gotcha. Sorry I can't be of more help, I'm not familiar with either the component or tying Cake into FB apps.
Travis Leleu