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
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
74322593
Commit
74322593
authored
Mar 03, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added support for rate limiting.
parent
9a23e313
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
16 additions
and
2 deletions
+16
-2
rest.md
docs/guide/rest.md
+1
-1
Controller.php
framework/rest/Controller.php
+15
-1
No files found.
docs/guide/rest.md
View file @
74322593
...
...
@@ -14,7 +14,7 @@ In particular, Yii provides support for the following aspects regarding RESTful
*
Authorization;
*
Support for HATEOAS;
*
Caching via
`yii\web\HttpCache`
;
*
Rate limiting
: TBD
*
Rate limiting
;
*
Searching and filtering: TBD
*
Testing: TBD
*
Automatic generation of API documentation: TBD
...
...
framework/rest/Controller.php
View file @
74322593
...
...
@@ -11,6 +11,7 @@ use Yii;
use
yii\web\Response
;
use
yii\web\UnauthorizedHttpException
;
use
yii\web\UnsupportedMediaTypeHttpException
;
use
yii\web\TooManyRequestsHttpException
;
use
yii\web\VerbFilter
;
/**
...
...
@@ -116,6 +117,7 @@ class Controller extends \yii\web\Controller
{
if
(
parent
::
beforeAction
(
$action
))
{
$this
->
authenticate
();
$this
->
checkRateLimit
(
$action
);
return
true
;
}
else
{
return
false
;
...
...
@@ -201,7 +203,6 @@ class Controller extends \yii\web\Controller
}
}
if
(
!
isset
(
$accessToken
)
||
!
Yii
::
$app
->
getUser
()
->
loginByAccessToken
(
$accessToken
))
{
if
(
!
isset
(
$accessToken
,
$authType
))
{
$authType
=
is_array
(
$this
->
authType
)
?
reset
(
$this
->
authType
)
:
$this
->
authType
;
...
...
@@ -214,6 +215,19 @@ class Controller extends \yii\web\Controller
}
/**
* Ensures the rate limit is not exceeded.
* You may override this method to log the API usage and make sure the rate limit is not exceeded.
* If exceeded, you should throw a [[TooManyRequestsHttpException]], and you may also send some HTTP headers,
* such as `X-RateLimit-Limit`, `X-RateLimit-Remaining`, `X-RateLimit-Reset`,
* to explain the rate limit information.
* @param \yii\base\Action $action the action to be executed
* @throws TooManyRequestsHttpException if the rate limit is exceeded.
*/
protected
function
checkRateLimit
(
$action
)
{
}
/**
* Serializes the specified data.
* The default implementation will create a serializer based on the configuration given by [[serializer]].
* It then uses the serializer to serialize the given data.
...
...
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