I’m using CakePhp 2.2 and I have this simple controller, named ProvidersController:
<?php
class ProvidersController extends AppController {
public $components = array('Session', 'Social');
public $layout = false;
public function facebook(){
$this->Social->auth('facebook', 'success', 'error');
$this->render(false);
}
public function google(){
$this->Social->auth('google', 'success', 'error');
$this->render(false);
}
private function success(){
}
private function error(){
}
}
?>
and this Component, named SocialComponent:
<?php
class SocialComponent extends Component {
public function auth($provider, $success, $error){
}
}
?>
as you can see I have created success() and error() methods inside the controller. Now I pass these names and I would like to call them back from the component.
I only pass the name of the callback, how to call them from the component?
Thank you!
The point of a component is re-usability across many controllers – if you’re trying to make it access a specific controller function, you should probably just be using controller methods, not a component.
BUT – you can always just do your logic in the component and pass back data (upon success or error), then check the results in the controller, and access whichever method you’d like based on those results.
Since your ‘success’ or ‘error’ logic is in the controller, I assume you don’t want it in the component… ie it’s different per use of the component. In that case, all you really want the component to do is do the logic and let you know how it went (return data).