用户
 找回密码
 入住 CI 中国社区
搜索
查看: 1237|回复: 0
收起左侧

[版本 4.x] 请教一个4.0.0-alpha.2路由问题

[复制链接]
发表于 2018-11-28 15:40:34 | 显示全部楼层 |阅读模式
最近下了CI 4.0.0-alpha.2 ,在使用过程发现一个问题,在uriProtocol 为 PATH_INFO 模式时,未使用路由规则,在自动识别路由会找不到控制器和方法。例如请求 www.tmp.com/main/test 应该会定位在application 中 Main 控制器中的 test 方法,但是请求的时候会出现404 错误,后面我看了框架源码,发现在未使用路由规则时,会调用system router Router.php 中的 autoRoute 方法, 其中的一段代码
PHP复制代码
 
    if (strpos($this->controller, '\\') === false && strlen($this->collection->getDefaultNamespace()) > 1)              {
                        //原来的代码
                        //$this->controller = str_replace('/', '\\', $this->collection->getDefaultNamespace() . $this->directory . $this->controller);
                        //修改后,只是将$this->directory 去掉就可以了。
                       $this->controller = str_replace('/', '\\', $this->collection->getDefaultNamespace() . $this->controller);
                }
 
复制代码



我打印调试的时候发现  $this->collection->getDefaultNamespace()  会在项目 config routes 设置的 DefaultNamespace 后面加多一个"\",之后再拼接上 $this->directory 和 $this->controller 再将 "/" 替换成 "\\" ,会使得 $this->controller 多出了一个 \ 。(即 请求main/test  $this->controller 会变为 App\Controllers\\Main 去掉$this->directory 后就变 App\Controllers\Main 就可以了) ,我猜想是不是为了将默认命名空间和后面类名隔开,前后都加了 "\" 所以出现这个问题?

本版积分规则