Yii2.0 RESTful Web Services (4)

Source: Internet
Author: User
Tags yii

Routing

As resources and controller classes are prepared, you can use URLs such as http://localhost/index.php?r=user/create access resources, similar to what you can do with normal Web application practices.

In practice, you usually use beautiful URLs and take advantage of the HTTP verbs. For example, POST /users a request means an access user/create action. This can be done easily by configuring urlManager the application components as follows:

' Urlmanager '= [' Enableprettyurl '=true,' enablestrictparsing '=true,' Showscriptname '=false,' Rules '= = [[' class '=' Yii\rest\urlrule ',' Controller '=' user '],    ],]

Setting Enableprettyurl to true, such as accessing www.xxxxx.com, does not show www.xxxxx.com/site/index.php directly but requires adding a corresponding configuration in the rules.

Set Enablestrictparsing to True, which indicates that the access URL must match at least one routing rule, otherwise it is considered invalid and takes effect after setting Enableprettyurl to true.

Set Showscriptname to True to indicate that the URL retains the portal script index.php.

compared to URL-managed Web applications The main new thing above is to request yii\rest\urlrule through the RESTful API. This special URL rule class will establish a complete set of sub-URL rules to support Routing and URL creation of the specified controller. For example, the above code is roughly in accordance with the following rules

:

[' Put,patch users/<id> '=' User/update ',' DELETE users/<id> '=' User/delete ',' Get,head users/<id> '=' User/view ',' POST users '=' User/create ',' Get,head users '=' User/index ',' users/<id> '=' User/options ',' users '=' User/options ',]

The rule supports the following API end:

  • GET /users: List all users on a per-page basis;
  • HEAD /users: Displays the summary information of the user list;
  • POST /users: Create a new user;
  • GET /users/123: Returns the user's 123 details;
  • 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.

You can configure  only    except   option to explicitly list which behaviors are supported and which behaviors are disabled. For example,

[ ' class '  =  ' Yii\rest\urlrule ' ,  controller '  =  ' user ' ,  ' except '  = = [ ' delete ' ,  ' create ' ,  ' update ' ],], 

If Usercontroller is inherited from Activecontroller, the corresponding method of disabling can also be declared through the Yii\rest\activecontroller::actions () method (Yii2.0 RESTful Controller related)

You can also configurepatternsOrextraPatternsRedefine an existing schema or add a new schema that is supported by this rule. For example, through the endGET /users/searchcan support new behaviorsearch, configure as followsextraPatternsOptions

[ ' class '  =  ' Yii\rest\urlrule ' ,  controller '  =  ' user ' ,  ' extrapatterns '  = = [ ' GET search '  =  ' search ' ,], 

You may have noticed the controller Iduser appears in plural form in users end. This is because Yii\rest\urlrule is able to use the end of the fully automatic complex controller ID for them. You can disable this behavior by setting Yii\rest\urlrule::p luralize to False, if you want to use some special names you can configure the Yii\rest\urlrule::controller property.

The value of Yii\rest\urlrule::controller is usually prefixed directly to the user configured in the Urlmanager in the rules, and of course Yii2.0 provides two custom methods, one of which is to pass in an array of key values such as the yii\rest\urlrule::controller assignment is "[' U ' + ' user ']" so that subsequent accesses will treat U as the controller ID and the other is to enter an array such as "[' User ', ' Player ']" This generates a routing rule that corresponds to all incoming values. Yii has a relatively simple way to handle this on the routing rules, and in a simple and crude way, yii can sacrifice a certain amount of development convenience to improve the performance of routing rules at the code level, such as using regular expressions directly, but Yii does not. Yii caches all routes at Urlmanager::init (), which stabilizes performance and ease of development.

Yii2.0 RESTful Web Services (4)

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.