1. apicontroller.php
<?php
namespace App\controllers;
Use Yii\web\response;
Use Yii\rest\activecontroller;
Class Apicontroller extends activecontroller{
/**
* Set the returned data to JSON format
* {@inheritDoc}
* @see \yii\rest\controller::behaviors ()
*/
Public Function Behaviors ()
{
$behaviors =parent::behaviors ();
$behaviors [' Contentnegotiator '] [' formats '] [' text/html '] = Response::format_json;
return $behaviors;
}
/**
* Reset default method for API-based seed
* {@inheritDoc}
* @see \yii\rest\activecontroller::actions ()
*/
Public function actions () {
return [];
}
}
2. usercontroller.php
<?php
namespace App\controllers;
Use Yii;
Use App\models\user;
Use app\components\utility;
Class Usercontroller extends apicontroller{
/**
* Define Model
*/
Public $modelClass = ' app\models\user ';
/**
* Filter data receive mode
* {@inheritDoc}
* @see \yii\rest\activecontroller::verbs ()
*/
protected function verbs () {
return [
' Login ' =>[' post ',
' Checklogin ' =>[' get ', ' post ',
];
}
/**
* Login
*/
Public Function Actionlogin () {
return [' OK ' = ' login '];
}
/**
* Verify if login
*/
Public Function Actionchecklogin () {
Return ['ok'='checklogin'];
}
}
3. installation of the Restful API test tool PostMan
Now the Chrome extension can only be installed from its store, but I found that the store was walled and couldn't open.
Method:
⑴ Change the postman_v3.0.17.crx of the attachment to the suffix named postman_v3.0.17.rar
⑵ unzip rar, get directory postman_v3.0.17
⑶ Modify subdirectories in the directory _metadata into metadata
⑷ in Chrome, open the extension interface chrome://extensions/
(4.1) In the upper-right corner, select [] developer Mode
(4.2) load the extension being developed
OK, success.
4. in the PostMan URL , enter:
127.0.0.1/user/login or 127.0.0.1/users/login, returns:' OK ' = ' login '. Because it was configured in api_cfg.php :' POST users/login ' = ' user/login ', so access users/ Login will be redirected to user/login.
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7F/62/wKioL1cdYAzACifnAAA3PTGoAck586.png "title=" 1.png " alt= "Wkiol1cdyazacifnaaa3ptgoack586.png"/>
This article is from the "Programming Art" blog, so be sure to keep this source http://itsart.blog.51cto.com/1005243/1767374
Introduction to RESTFULAPI development of YII2 (2)-1th Restfulapi