首页 > 开发 > PHP > 正文

Yii2创建控制器(createController)方法详解

2024-05-04 22:29:00
字体:
来源:转载
供稿:网友

本文实例讲述了Yii2创建控制器(createController)方法。分享给大家供大家参考,具体如下:

yii中创建控制器的是在application中的request通过UrlManager解析得出路由信息的,然后再由yii/base/Module中的

public function runAction($route, $params = [])

方法来创建控制器,最后由控制器再执行相应的动作。

首先得明确,Yii中的路由分三种情况:

第一种是带有模块的(module id/controller id/action id),

第二种是带有命名空间(子目录)的(sub dir)/controller id/action id)

第三种是只有控制器和动作的(controller id/action id)

这三个有优先顺序,所以在创建控制器的时候,也是先查看是否是模块类型的路由,如果是,则获取这个模块,再由这个模块来创建控制器

接着再判断是否是第二种带有命名空间的。

public function createController($route){  //如果路由为空,则使用默认的路由  if ($route === '') {    $route = $this->defaultRoute;  }  // double slashes or leading/ending slashes may cause substr problem  //去掉首尾的反斜杠(“/”),如果路由中包含有“//”,则返回false创建失败。  $route = trim($route, '/');  if (strpos($route, '//') !== false) {    return false;  }  /*   * 路由分三种情况,   * 一种是带模块id的(module id/controller id/action id),   * 一种是有命名空间(子目录)的(sub dir)/controller id/action id)   * 一种是只有控制器和动作的(controller id/action id)   * 所以在这里要根据第一个“/”分隔成两部分,$id和$route信息,   */  if (strpos($route, '/') !== false) {    list ($id, $route) = explode('/', $route, 2);  } else {    $id = $route;    $route = '';  }  // module and controller map take precedence  /*   * 查看这个id是否是模块,如果是模块,则再用这个模块来创建控制器。   * 所以,在如果一个控制器的名称和模块名称重复的话会优先创建模块里面的控制器。   *   * 如果有url: http://www.yii2.com/index.php?r=test/index   * 本来是打算访问application中的控制器里面的test控制器,执行index动作的。   *   * 然而如果有个模块的名字为test,里面有个IndexController   *   * 根据上面会生成$id=test,$route=index   *   * 由于在下面查找存在这个模块,所以会执行这个test模块下面的index控制器,   * 而不会执行application里面的test控制器的index动作   */  $module = $this->getModule($id);  if ($module !== null) {    return $module->createController($route);  }  //如果在controllerMap数组中指定了控制器映射,会优先根据这个里面的映射来创建控制器  if (isset($this->controllerMap[$id])) {    $controller = Yii::createObject($this->controllerMap[$id], [$id, $this]);    return [$controller, $route];  }  /*   * 如果这个时候$route中还有“/”,也就是说原来的路由为home/index/aa   * $id:home(不是模块)   * $route:index/aa   * 由于经过上面得知home不为模块,所以这个为命名空间(子目录),   *   * 再经过下面处理后为   * $id:home/index 命名空间(子目录)home下面的index控制器   * $route:aaa   *   */  if (($pos = strrpos($route, '/')) !== false) {    $id .= '/' . substr($route, 0, $pos);    $route = substr($route, $pos + 1);  }    /*     * $id:home/index     * $route:aaa     */  $controller = $this->createControllerByID($id);  if ($controller === null && $route !== '') {      //如果创建失败,再加上route作为id再次创建    $controller = $this->createControllerByID($id . '/' . $route);    $route = '';  }  return $controller === null ? false : [$controller, $route];}            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表