Creating a controller

Yii provides two base classes: \yii\rest\Controller and \yii\rest\ActiveController that we can extend when we are creating a new controller for RESTful web services.

Both of these classes contain the following useful common features, in execution order:

  1. The response output as required from the request (content negotiator).
  2. The HTTP method validation.
  3. Authentication.
  4. Rate limiting.

The second class \yii\rest\ActiveController adds more functionalities through ActiveRecord, such as handling user authorization and a set of already existing actions: index, view, create, update, delete, and options.

We will see that Yii provides all the necessary information to get the response status and content through the body and HTTP header.

Let's ...

Get Yii2 By Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.