You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1013 B
43 lines
1013 B
5 years ago
|
<?php
|
||
|
|
||
|
namespace app\behaviors;
|
||
|
|
||
|
use yii\base\Behavior;
|
||
|
use yii\web\Controller;
|
||
|
|
||
|
class BaseBehavior extends Behavior
|
||
|
{
|
||
|
public function beforeActionBase($event)
|
||
|
{
|
||
|
if (static::matchRoutes($this->only_routes)) {
|
||
|
return $this->beforeAction($event);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public function events()
|
||
|
{
|
||
|
return [
|
||
|
Controller::EVENT_BEFORE_ACTION => 'beforeActionBase'
|
||
|
];
|
||
|
}
|
||
|
|
||
|
public static function matchRoutes($routes)
|
||
|
{
|
||
|
$route = \Yii::$app->requestedRoute;
|
||
|
if (is_array($routes)) {
|
||
|
foreach ($routes as $r) {
|
||
|
if ($r == $route) {
|
||
|
return true;
|
||
|
}
|
||
|
$r = str_replace('/', '\/', $r);
|
||
|
$r = str_replace('*', '.*', $r);
|
||
|
$r = "/^{$r}$/";
|
||
|
if (preg_match($r, $route, $res)) {
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
}
|