Skip to content

Commit 3d910a4

Browse files
committed
feat: 增加 BootstrapInterface 使用方式
1 parent eabdc1c commit 3d910a4

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

EventBootstrap.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* author : forecho <caizhenghai@gmail.com>
4+
* createTime : 2020/2/17 9:12 下午
5+
* description:
6+
*/
7+
8+
namespace yiier\returnUrl;
9+
10+
use yii\base\BootstrapInterface;
11+
use yii\base\Controller;
12+
use yii\base\Event;
13+
14+
class EventBootstrap implements BootstrapInterface
15+
{
16+
public $uniqueIds;
17+
18+
public function bootstrap($app)
19+
{
20+
$controllerEvents = [
21+
Controller::EVENT_BEFORE_ACTION,
22+
];
23+
24+
foreach ($controllerEvents as $eventName) {
25+
Event::on(Controller::class, $eventName, function ($event) {
26+
/** @var Controller $controller */
27+
$controller = $event->sender;
28+
$controller->attachBehavior('return-url', [
29+
'class' => ReturnUrl::class,
30+
'uniqueIds' => $this->uniqueIds
31+
]);
32+
});
33+
}
34+
}
35+
}

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@ to the require section of your `composer.json` file.
3232
Usage
3333
-----
3434

35+
## Method One (方式一,推荐)
36+
37+
you need to include it in config in bootstrap section:
38+
39+
```php
40+
return [
41+
'bootstrap' => [
42+
'yiier\returnUrl\EventBootstrap',
43+
],
44+
];
45+
```
46+
47+
48+
## Method Two (方式二)
49+
3550
In your controller add ReturnUrl filter to behaviors:
3651

3752
```php

ReturnUrl.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
* createTime : 2016/3/16 10:14
66
* description:
77
*/
8-
namespace yiier\ReturnUrl;
8+
9+
namespace yiier\returnUrl;
910

1011
use Yii;
1112
use yii\base\ActionFilter;
@@ -20,6 +21,7 @@ class ReturnUrl extends ActionFilter
2021
/**
2122
* @param \yii\base\Action $action
2223
* @return bool
24+
* @throws \yii\base\InvalidConfigException
2325
*/
2426
public function beforeAction($action)
2527
{

0 commit comments

Comments
 (0)