Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Rotua Panjaitan
yii2
Commits
b4a9ea8f
Commit
b4a9ea8f
authored
Aug 12, 2014
by
Vadim Belorussov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add rest-routing.md to translate into Russian
parent
a4e4c1e0
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
78 additions
and
0 deletions
+78
-0
rest-routing.md
docs/guide-ru/rest-routing.md
+78
-0
No files found.
docs/guide-ru/rest-routing.md
0 → 100644
View file @
b4a9ea8f
Routing
=======
With resource and controller classes ready, you can access the resources using the URL like
`http://localhost/index.php?r=user/create`
, similar to what you can do with normal Web applications.
In practice, you usually want to enable pretty URLs and take advantage of HTTP verbs.
For example, a request
`POST /users`
would mean accessing the
`user/create`
action.
This can be done easily by configuring the
`urlManager`
application component in the application
configuration like the following:
```
php
'urlManager'
=>
[
'enablePrettyUrl'
=>
true
,
'enableStrictParsing'
=>
true
,
'showScriptName'
=>
false
,
'rules'
=>
[
[
'class'
=>
'yii\rest\UrlRule'
,
'controller'
=>
'user'
],
],
]
```
Compared to the URL management for Web applications, the main new thing above is the use of
[
[yii\rest\UrlRule
]
] for routing RESTful API requests. This special URL rule class will
create a whole set of child URL rules to support routing and URL creation for the specified controller(s).
For example, the above code is roughly equivalent to the following rules:
```
php
[
'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'
,
]
```
And the following API endpoints are supported by this rule:
*
`GET /users`
: list all users page by page;
*
`HEAD /users`
: show the overview information of user listing;
*
`POST /users`
: create a new user;
*
`GET /users/123`
: return the details of the user 123;
*
`HEAD /users/123`
: show the overview information of user 123;
*
`PATCH /users/123`
and
`PUT /users/123`
: update the user 123;
*
`DELETE /users/123`
: delete the user 123;
*
`OPTIONS /users`
: show the supported verbs regarding endpoint
`/users`
;
*
`OPTIONS /users/123`
: show the supported verbs regarding endpoint
`/users/123`
.
You may configure the
`only`
and
`except`
options to explicitly list which actions to support or which
actions should be disabled, respectively. For example,
```
php
[
'class'
=>
'yii\rest\UrlRule'
,
'controller'
=>
'user'
,
'except'
=>
[
'delete'
,
'create'
,
'update'
],
],
```
You may also configure
`patterns`
or
`extraPatterns`
to redefine existing patterns or add new patterns supported by this rule.
For example, to support a new action
`search`
by the endpoint
`GET /users/search`
, configure the
`extraPatterns`
option as follows,
```
php
[
'class'
=>
'yii\rest\UrlRule'
,
'controller'
=>
'user'
,
'extraPatterns'
=>
[
'GET search'
=>
'search'
,
],
```
You may have noticed that the controller ID
`user`
appears in plural form as
`users`
in the endpoints.
This is because
[
[yii\rest\UrlRule
]
] automatically pluralizes controller IDs for them to use in endpoints.
You may disable this behavior by setting
[
[yii\rest\UrlRule::pluralize
]
] to be false, or if you want
to use some special names you may configure the
[
[yii\rest\UrlRule::controller
]
] property.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment