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:
The response output as required from the request (content negotiator).
The HTTP method validation.
Authentication.
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 create a controller to extend \yii\rest\Controller
or rather without ActiveRecord. Create a new controller in api/controllers/TestRestController.php
:
<?php namespace api\controllers; use yii\rest\Controller; class TestRestController extends Controller...