In Craft2, when making an ajax request to a plugin controller action, you would use the returnJson() method which is part of the BaseController class - which your plugin controllers extend.

This sets some headers, echo's the response and kills any further processing.

public function actionDoStuff() {

    $response = (object)[
        "message" => "Look at me! I'm JSON!"


In Craft 3/Yii 2 I'm having trouble finding the equivalent.

1 Answer 1


I wasn't rtfm'ing hard enough. In the docs for \yii\web\Controller, which is what the \craft\web\Controller class extends (which is what your plugin controllers extend) there is a method asJson().

return $this->asJson($response);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.