Answer the question
In order to leave comments, you need to log in
How to handle an ajax request of a widget in Yii by the widget itself?
I put repeating blocks into widgets in my project, some of them exchange information using ajax requests. The question arose of how it is possible to transfer the functionality of processing ajax requests from controllers to a widget, so that it would be completely independent and independent?
Answer the question
In order to leave comments, you need to log in
Until I came up with this solution. Created one Widget controller, and through it I launch the widget method.
For example, an ajax request will go to www.site.ru/widget/index?widget=rating&method=recalc Its ajaxRecalc method of the Rating widget will be processed
class WidgetController extends Controller{
public function actionIndex($widget, $method){
Yii::import('application.extensions.'.$widget.'.'.$widget);
$obj = new $widget();
$method = 'ajax'.$method;
if(!method_exists($obj, $method)){
throw new CHttpException(500);
}
$obj->$method();
}
public function filterInit($filterChain){
if(!Is::ajax()){
throw new CHttpException(500);
}
$filterChain->run();
}
public function filters(){
return array('init');
}
}
ajax is needed when you need to access the controller from the user's browser without reloading the page. And from the widget to the widget, you need to climb using php.
Or you didn't quite accurately describe your tasks.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question