Introduction to RESTFULAPI development of YII2 (2)-1th Restfulapi

Source: Internet
Author: User
Tags yii

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.