Method Details

asJson()
public method
(available since version 2.0.11)

Send data formatted as JSON.

This method is a shortcut for sending data formatted as JSON. It will return
the response application component after configuring
the format and setting the data that should
be formatted. A common usage will be:

This method is a shortcut for sending data formatted as XML. It will return
the response application component after configuring
the format and setting the data that should
be formatted. A common usage will be:

The method will trigger the EVENT_BEFORE_ACTION event. The return value of the method
will determine whether the action should continue to run.

In case the action should not run, the request should be handled inside of the beforeAction code
by either providing the necessary output or redirecting the request. Otherwise the response will be empty.

If you override this method, your code should look like the following:

publicfunctionbeforeAction($action){
// your custom code here, if you want the code to run before action filters,// which are triggered on the [[EVENT_BEFORE_ACTION]] event, e.g. PageCache or AccessControlif (!parent::beforeAction($action)) {
returnfalse;
}
// other custom code herereturntrue; // or false to not run the action
}

This method is invoked by yii\base\Action when it begins to run with the given parameters.
This method will check the parameter names that the action requires and return
the provided parameters according to the requirement. If there is any missing parameter,
an exception will be thrown.

This method is similar to renderPartial() except that it will inject into
the rendering result with JS/CSS scripts and files which are registered with the view.
For this reason, you should use this method instead of renderPartial() to render
a view to respond to an AJAX request.