1 版本
// yii\BaseYii\getVersion
public static function getVersion()
{
return '2.0.10';
}
2 示例
AccessControl是框架自带的,位于yii\filters目录下
class SiteController extends Controller
{
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['login', 'error', 'register', 'offline'],
'allow' => true,
],
[
'allow' => true,
'roles' => ['@'],
]
]
]
];
}
}
3 客户端请求后的运行流程
具体运行流程请参考Yii2分析运行流程
http://blog.csdn.net/alex_my/article/details/54142711
最终会在yii\base\Module::runAction中
创建controller, action
然后执行$controller->runAction
public function runAction($route, $params = [])
{
$parts = $this->createController($route);
if (is_array($parts))
{
list($controller, $actionID) = $parts;
Yii::$app->controller = $controller;
$result = $controller->runAction($actionID, $params);
...
}
...
}
runAction中的流程请参考Yii理解Controller
http://blog.csdn.net/alex_my/article/details/54138744
在yii\base\Controller::runAction中
会执行 this->beforeAction —> this->ensureBehaviors()
将SiteController::behaviors()中的行为绑定到SiteController上
public function ensureBehaviors()
{
if ($this->_behaviors === null) {
$this->_behaviors = [];
foreach ($this->behaviors() as $name => $behavior) {
$this->attachBehaviorInternal($name, $behavior);
}
}
}
private function attachBehaviorInternal($name, $behavior)
{
if (!($behavior instanceof Behavior))
{
$behavior = Yii::createObject($behavior);
}
if (is_int($name))
{
$behavior->attach($this);
$this->_behaviors[] = $behavior;
}
else
{
if (isset($this->_behaviors[$name]))
{
$this->_behaviors[$name]->detach();
}
// ---------------- 这个名为access的行为将会执行到这里
$behavior->attach($this);
$this->_behaviors[$name] = $behavior;
}
return $behavior;
}
$behaviors实际上是AccessControl的对象,attach是其基类ActionFilter中的函数
// yii\base\ActionFilter::attach
public function attach($owner)
{
$this->owner = $owner;
$owner->on(Controller::EVENT_BEFORE_ACTION, [$this, 'beforeFilter']);
}
将会执行ActionFilter::beforeFilter
public function beforeFilter($event)
{
...
$event->isValid = $this->beforeAction($event->action);
...
}
AccessControl重载了beforeAction函数
// yii\filters\AccessControl::beforeAction
public function beforeAction($action)
{
$user = $this->user;
$request = Yii::$app->getRequest();
// 这些就是在SiteController中定义的规则
foreach ($this->rules as $rule)
{
if ($allow = $rule->allows($action, $user, $request))
{
return true;
}
elseif ($allow === false)
{
if (isset($rule->denyCallback))
{
call_user_func($rule->denyCallback, $rule, $action);
}
elseif ($this->denyCallback !== null)
{
call_user_func($this->denyCallback, $rule, $action);
}
else
{
$this->denyAccess($user);
}
return false;
}
}
if ($this->denyCallback !== null)
{
call_user_func($this->denyCallback, null, $action);
}
else
{
$this->denyAccess($user);
}
return false;
}
4 简述
简单的说就是会在执行controller中的action(比如SiteController::actionIndex)之前, 会先执行这些behaviors, 如果没有通过, 则action不会继续执行下去。