问答

thinkphp控制器不存在问题

作者:admin 2021-04-10 我要评论

在tp框架中,不走路由的情况下大驼峰的控制器名会被先转成小写最后把首字母变成大写,如 GetType 最终会被转成 Gettype 我看了tp的源码判断控制器是否存在用的 c...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)

在tp框架中,不走路由的情况下大驼峰的控制器名会被先转成小写最后把首字母变成大写,如 GetType 最终会被转成Gettype 我看了tp的源码判断控制器是否存在用的class_exists(),但是这个方法不是不区分类名的大小写吗?请大佬帮忙解答

tp中判断控制器是否存在的源码

????

/?*?实例化(分层)控制器?格式:[模块名/]控制器名

?????*?@access?public

?????*?@param??string?$name??????????????资源地址

?????*?@param??string?$layer?????????????控制层名称

?????*?@param??bool???$appendSuffix??????是否添加类名后缀

?????*?@param??string?$empty?????????????空控制器名称

?????*?@return?object

?????*?@throws?ClassNotFoundException

?????*/

????public?function?controller($name,?$layer?=?'controller',?$appendSuffix?=?false,?$empty?=?'')

????{

????????list($module,?$class)?=?$this->parseModuleAndClass($name,?$layer,?$appendSuffix);

????????//?dump(class_exists("app\index\controller\Gettype"));?返回false

????????//?dump(class_exists('app\index\controller\GetType'));?返回true

????????if?(class_exists($class))?{

????????????return?$this->make($class,?true);

????????}?elseif?($empty?&&?class_exists($emptyClass?=?$this->parseClass($module,?$layer,?$empty,?$appendSuffix)))?{

????????????return?$this->make($emptyClass,?true);

????????}

????????throw?new?ClassNotFoundException('class?not?exists:'?.?$class,?$class);

????}

image

我在tp中的index控制器中测试class_exists()的返回值

<?php
namespace app\index\controller;

use think\facade\Config;

use think\Request;

use think\Controller;

class Index extends Controller
{
    public function index()
    {
        return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V5.1<br/><span style="font-size:30px">12载初心不改(2006-2018) - 你值得信赖的PHP框架</span></p></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="/uploads/allimg/210410/0Z51Qb6-2.jpg"></script><think id="eab4b9f840753f8e7"></think>';
    }
    public function t()
    {
        // return $this->fetch();
        dump(class_exists("app\index\controller\gettype"));//返回false
        dump(class_exists("app\index\controller\GetType"));//返回true
    }
}

不在tp框架中使用class_exists()

<?php
namespace app\index\controller;
class GetType
{

}
var_dump(class_exists('app\index\controller\gettype'));//返回true
var_dump(class_exists('app\index\controller\GetType'));//返回true
###

先说结论,看起来应该是因为 autoload 的问题,因为当使用 composer 的项目,引入 composer 的 autoload 后,composer 会在根绝约定,PSR-4,PSR-0,CLASS MAP 等,进行自动的 require 。只是你在代码中没看到 require。

话说回来,为什么这里会不存在呢?

因为按照 PSR-4,命名空间和文件是对应的,而在 Linux 平台下,文件、目录是区分大小写的,所以 app\index\controller\gettype 找不到对应的对象 gettype 的 php 文件。

而在 Windows 平台下是不区分大小写的,所以不存在这个问题。

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • nginx响应速度很慢

    nginx响应速度很慢

  • 点击选中的多选框,会在已选那一栏显示

    点击选中的多选框,会在已选那一栏显示

  • PHP 多态的理解

    PHP 多态的理解

  • 关于C语言中static的问题

    关于C语言中static的问题

腾讯云代理商
海外云服务器