简体   繁体   English

如何在CakePhp组件中调用回调?

[英]How to call the callback in a CakePhp Component?

I'm using CakePhp 2.2 and I have this simple controller, named ProvidersController : 我正在使用CakePhp 2.2,并且有一个名为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 : 这个组件名为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. 如您所见,我已经在控制器内部创建了success()和error()方法。 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! 谢谢!

Have a look at the source code of the SecurityComponent in CakePHP, it has an identical situation with its blackHoleCallback. 看一下CakePHP中SecurityComponent源代码 ,它与blackHoleCallback情况相同。

They use a helper function SecurityComponent::_callback() which uses PHP's call_user_func_array() 他们使用了一个辅助函数SecurityComponent::_callback() ,它使用了PHP的call_user_func_array()

protected function _callback(Controller $controller, $method, $params = array()) {
    if (is_callable(array($controller, $method))) {
        return call_user_func_array(array(&$controller, $method), empty($params) ? null : $params);
    } else {
        return null;
    }
}

You can use the same pattern to pass callbacks into your component. 您可以使用相同的模式将回调传递到组件中。

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). 在这种情况下,您真正​​希望组件执行的只是执行逻辑,并让它知道它的运行方式(返回数据)。

//Controller
//..
public function facebook(){ 
    $results = $this->Social->auth('facebook', 'success', 'error');
    if($results['error']) $this->Social->error();
    $this->render(false);
}

private function success(){ }

private function error(){ }
//...


//Component
//...
public function auth($provider, $success, $error){
    $results = array();
    //do something
    $results['error'] = true;
    return $results;
}
//...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM