Questions about the Yii2 REST API

Source: Internet
Author: User
Tags yii

First, you need to add a. htaccess file under the basic/web/folder

This entry will automatically access the index.php file, the URL will not be garbled

<mod_rewrite.c>Options +followsymlinksindexignore */*rewriteengine on# If a directory or a file exists, use it directlyrewritecond%{request_filename}!-frewritecond%{request_filename}!-d# OT Herwise forward it to index.phprewriterule. index.php</ifmodule>

Let's take a look at what's in the official documentation.

First, create a Yii\db\activerecord classapp\models\User来访问user表

Then, create a controller class app\controllers\UserController as follows,

<? phpnamespace app\controllers;  Use Yii\rest\activecontroller; class extends activecontroller{    public$modelClass = ' App\models\user ';}

Then, modify the configuration of the urlManager component in your application configuration:

 ' Urlmanager ' => [ ' enableprettyurl ' =    true , ' enablestrictparsing ' = = true ,        ' Showscriptname ' = false , ' rules ' => [ [ ' class ' = ' Yii\rest\urlrule ', ' controller ' = ' user '],, ]
    // used to indicate whether Urlmanager is enabled for URL beautification, known as the path format URL in Yii1.1,    or renamed in Yii2.0.    //not enabled by default. But in the actual use, especially the product environment, generally will be enabled. public    $enablePrettyUrlfalse;     // whether to enable strict parsing, such as enabling strict parsing, requires that the current request should match at least 1 routing rules,    //Otherwise it is considered an invalid route.    //This option is only valid if Enableprettyurl is enabled. public    $enableStrictParsingfalse;
// Specifies whether the URL is in the reserved entry script index.php     Public $showScriptName true;

After changing to the above settings, Http://localhost/basic/web/users can access the API directly (note that it automatically adds a s for you)

Now let's take a look at a urlmanager configuration rule:

' Urlmanager ' = [          ' Enableprettyurl ' =true,//true to open beautify' Enablestrictparsing ' =true,//true to enable strict parsing, requires that the current request should match at least 1 routing rules, not to route 404, for example http://localhost/basic/web/will 404;false is to find the index' Showscriptname ' =false,//Specifies whether the URL is in the reserved entry script index.php' Rules ' = [                [' Class ' = ' Yii\rest\urlrule ', ' controller ' = ' user ', ' pluralize ' =true],//true requires access to users, false requires access to user, default is True],        ],
    • GET /users: List all users on a per-page basis
    • HEAD /users: Displays summary information for the user list
    • POST /users: Create a new user
    • GET /users/123: Returns the details of user 123
    • HEAD /users/123: Displays overview information for user 123
    • PATCH /users/123and PUT /users/123 : Update user 123
    • DELETE /users/123: Delete User 123
    • OPTIONS /users: Displays verbs about /users the end support
    • OPTIONS /users/123: Displays verbs about /users/123 the end support

Questions about the Yii2 REST API

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.