Skip to content

Commit 4b00e6d

Browse files
committed
v0.1
1 parent dbf38ba commit 4b00e6d

File tree

4 files changed

+137
-6
lines changed

4 files changed

+137
-6
lines changed

.gitignore

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
1-
assets/*
2-
!assets/.gitignore
3-
protected/runtime/*
4-
!protected/runtime/.gitignore
5-
protected/data/*.db
6-
themes/classic/views/
1+
# phpstorm project files
2+
.idea
3+
4+
# netbeans project files
5+
nbproject
6+
7+
# zend studio for eclipse project files
8+
.buildpath
9+
.project
10+
.settings
11+
12+
# windows thumbnail cache
13+
Thumbs.db
14+
15+
# composer vendor dir
16+
/vendor
17+
18+
# composer itself is not needed
19+
composer.phar
20+
# composer.lock should not be committed as we always want the latest versions
21+
/composer.lock
22+
23+
# Mac DS_Store Files
24+
.DS_Store

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
Yii2 Login ReturnUrl filter
2+
===========================
3+
Keeps current URL in session for login actions so we can return to it if needed.
4+
5+
中文说明:登录之后自动跳转登录之前的页面
6+
7+
Installation
8+
------------
9+
10+
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
11+
12+
Either run
13+
14+
```
15+
php composer.phar require --prefer-dist yiier/yiier-return-url "*"
16+
```
17+
18+
or add
19+
20+
```
21+
"yiier/yiier-return-url": "*"
22+
```
23+
24+
to the require section of your `composer.json` file.
25+
26+
27+
Usage
28+
-----
29+
30+
In your controller add ReturnUrl filter to behaviors:
31+
32+
```php
33+
public function behaviors()
34+
{
35+
return [
36+
'returnUrl' => [
37+
'class' => 'yiier\returnUrl\ReturnUrl',
38+
'uniqueIds' => ['site/qrcode', 'site/login', 'user/security/auth'] // 过滤掉不需要的 controller/action
39+
],
40+
];
41+
}
42+
```
43+
44+
For access to previously visited url:
45+
46+
```php
47+
Yii::$app->user->getReturnUrl();
48+
```

ReturnUrl.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/**
4+
* author : forecho <caizhenghai@gmail.com>
5+
* createTime : 2016/3/16 10:14
6+
* description:
7+
*/
8+
namespace yiier\ReturnUrl;
9+
10+
use Yii;
11+
use yii\base\ActionFilter;
12+
13+
class ReturnUrl extends ActionFilter
14+
{
15+
/**
16+
* @var array
17+
*/
18+
public $uniqueIds = ['site/login'];
19+
20+
/**
21+
* @param \yii\base\Action $action
22+
* @return bool
23+
*/
24+
public function beforeAction($action)
25+
{
26+
if (Yii::$app->user->isGuest) {
27+
if (!(Yii::$app->request->getIsAjax() || $this->isValidate())) {
28+
Yii::$app->user->setReturnUrl(Yii::$app->request->getUrl());
29+
}
30+
}
31+
return true;
32+
}
33+
34+
/**
35+
* @return bool
36+
*/
37+
private function isValidate()
38+
{
39+
if (in_array(Yii::$app->controller->action->uniqueId, $this->uniqueIds)) {
40+
return true;
41+
}
42+
return false;
43+
}
44+
}

composer.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "yiier/yiier-return-url",
3+
"description": "Keeps current URL in session for login actions so we can return to it if needed.",
4+
"type": "yii2-extension",
5+
"keywords": ["yii2","filter","ReturnUrl",""],
6+
"license": "BSD-3-Clause",
7+
"authors": [
8+
{
9+
"name": "forecho",
10+
"email": "caizhenghai@gmail.com"
11+
}
12+
],
13+
"require": {
14+
"yiisoft/yii2": "*"
15+
},
16+
"autoload": {
17+
"psr-4": {
18+
"yiier\\returnUrl\\": ""
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)