zoukankan      html  css  js  c++  java
  • yii2框架随笔33

     今天来看yiiaseInlineAction

    namespace yiibase;
    
    use Yii;
    
    /**
     * InlineAction表示一个动作被定义为一个控制器的方法.
     *
     * The name of the controller method is available via [[actionMethod]] which
     * is set by the [[controller]] who creates this action.
     *
     * @author Qiang Xue <qiang.xue@gmail.com>
     * @since 2.0
     */
    class InlineAction extends Action
    {
        /**
         * @var string the controller method that this inline action is associated with
         */
        public $actionMethod;
    
    
        /**
         * @param string $id the ID of this action
         * @param Controller $controller the controller that owns this action
         * @param string $actionMethod the controller method that this inline action is associated with
         * @param array $config name-value pairs that will be used to initialize the object properties
         */
        public function __construct($id, $controller, $actionMethod, $config = [])
        {
            // 传入action的id,controller的实例,action方法的名称以及action的配置
            $this->actionMethod = $actionMethod;
            parent::__construct($id, $controller, $config);
        }
    
        /**
         * 这个动作用指定的参数运行.
         * 这种方法主要是由控制器调用.
         * @param array $params action parameters
         * @return mixed the result of the action
         */
        public function runWithParams($params)
        {
            // 获取action的参数,通过合并出入的参数和默认参数得出
            $args = $this->controller->bindActionParams($this, $params);
            Yii::trace('Running action: ' . get_class($this->controller) . '::' . $this->actionMethod . '()', __METHOD__);
            if (Yii::$app->requestedParams === null) {
                // 记录请求的参数
                Yii::$app->requestedParams = $args;
            }
    
            // 执行controller的这个action方法
            return call_user_func_array([$this->controller, $this->actionMethod], $args);
        }
    }
    
    public function __construct($id, $module, $config = [])
         {
    +        // 传入controller的id和所在的module的实例
             $this->id = $id;
             $this->module = $module;
             parent::__construct($config);
     @@ -117,6 +118,7 @@ public function actions()
          */
         public function runAction($id, $params = [])
         {
    +        // 创建action的实例
             $action = $this->createAction($id);
             if ($action === null) {
                 throw new InvalidRouteException('Unable to resolve the request: ' . $this->getUniqueId() . '/' . $id);
     @@ -125,6 +127,7 @@ public function runAction($id, $params = [])
             Yii::trace("Route to run: " . $action->getUniqueId(), __METHOD__);
     
             if (Yii::$app->requestedAction === null) {
    +            // 记录当前的action为requestedAction
                 Yii::$app->requestedAction = $action;
             }
     
     @@ -135,24 +138,31 @@ public function runAction($id, $params = [])
             $runAction = true;
     
             // call beforeAction on modules
    +        // 从外到里一层层执行module的beforeAction
             foreach ($this->getModules() as $module) {
                 if ($module->beforeAction($action)) {
    +                // 将执行成功的module放入到$modules中,顺序会颠倒
                     array_unshift($modules, $module);
                 } else {
    +                // 执行失败,就标记一下,并跳出循环
                     $runAction = false;
                     break;
                 }
             }
     
             $result = null;
     
    +        // modules的beforeAction执行成功而且controller本身的beforeAction也执行成功
             if ($runAction && $this->beforeAction($action)) {
                 // run the action
    +            // 执行action
                 $result = $action->runWithParams($params);
     
    +            // 执行controller本身的afterAction
                 $result = $this->afterAction($action, $result);
     
                 // call afterAction on modules
    +            // 从里到外一层层执行所有modules的beforeAction
                 foreach ($modules as $module) {
                     /* @var $module Module */
                     $result = $module->afterAction($action, $result);
     @@ -211,17 +221,25 @@ public function bindActionParams($action, $params)
         public function createAction($id)
         {
             if ($id === '') {
    +            // 如果action的id为空,就是用默认的action
                 $id = $this->defaultAction;
             }
     
    +        // 获取actions方法中的定义的actionMap
             $actionMap = $this->actions();
             if (isset($actionMap[$id])) {
    +            // 如果action的id在actionMap中,就去创建这个action
                 return Yii::createObject($actionMap[$id], [$id, $this]);
             } elseif (preg_match('/^[a-z0-9\-_]+$/', $id) && strpos($id, '--') === false && trim($id, '-') === $id) {
    +            // 如果id符合命名规范,并且不存在--,而且两边不存在-
    +            // 必须是数字/小写字母/右斜线/中划线/下划线组成的的
    +            // 用与拼接controller类名类似的方法拼接action方法的名称
                 $methodName = 'action' . str_replace(' ', '', ucwords(implode(' ', explode('-', $id))));
                 if (method_exists($this, $methodName)) {
    +                // 如果方法存在,就new一个ReflectionMethod实例
                     $method = new ReflectionMethod($this, $methodName);
                     if ($method->isPublic() && $method->getName() === $methodName) {
    +                    // 如果方法是public的,就new一个InlineAction返回
                         return new InlineAction($id, $this, $methodName);
                     }
                 }
     @@ -297,9 +315,13 @@ public function afterAction($action, $result)
          */
         public function getModules()
         {
    +        // 用当前controller的module组成modules的数组
             $modules = [$this->module];
             $module = $this->module;
    +        // 遍历module的module,直到为空
             while ($module->module !== null) {
    +            // array_unshift — 在数组开头插入一个或多个单元
    +            // 将外面的module插入到modules数组的开头
                 array_unshift($modules, $module->module);
                 $module = $module->module;
             }
  • 相关阅读:
    PCLVisualizer可视化类
    【Leetcode】课程表(拓扑排序)
    不是充许的静态以太网地址,它与vmware保留的mac地址冲突
    vmware虚拟机linux添加硬盘后先分区再格式化操作方法
    卸载mysql时,提示libmysqlclient.so.16()(64bit) is needed by (installed) postfix
    创建mysql数据库,在新数据库中创建表,再尝试删除表
    忘记linux下的mysql密码,需要重新创建密码123456
    创建mysql表
    程序员的冷笑话 python版本
    python的__mro__与__slot__
  • 原文地址:https://www.cnblogs.com/taokai/p/5506677.html
Copyright © 2011-2022 走看看