S
S
Sergey2015-02-09 09:19:57
JavaScript
Sergey, 2015-02-09 09:19:57

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

2 answer(s)
S
Sergey, 2015-02-09
@frost18 Asker

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');
    }
}

M
Maxim Timofeev, 2015-02-09
@webinar

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 question

Ask a Question

731 491 924 answers to any question