ThinkPHP5修改default_controller提示控制器不存在的一种解决方法

最近在捣鼓ThinkPHP5,发现了一个很奇怪的问题,也就是我把默认的控制器从Index文件改为自己的TestController后,提示我控制器不存在,首先我先展示自己是怎么改的:
新的控制器TestController:

<?php
namespace app\index\controller;

class TestController
{
    public function index()
    {
        return 'Hello Dude';
    }
}

并且修改app.php文件的这个位置:

// 默认控制器名
    'default_controller'     => 'TestController',

报错如下:

[0] HttpException in Module.php line 107
控制器不存在:app\index\controller\Testcontroller
    public function run()
    {
        // 实例化控制器
        try {
            $instance = $this->app->controller($this->controller,
                $this->app->config('app.url_controller_layer'),
                $this->app->config('app.controller_suffix'),
                $this->app->config('app.empty_controller'));
        } catch (ClassNotFoundException $e) {
            throw new HttpException(404, 'controller not exists:' . $e->getClass());
        }

        // 获取当前操作名
        $action = $this->actionName . $this->app->config('app.action_suffix');

        if (is_callable([$instance, $action])) {
            // 执行操作方法
            $call = [$instance, $action];

Call Stack
in Module.php line 107
at Module->run() in Url.php line 31
at Url->run() in App.php line 378
at App->think\{closure}(object(Request), object(Closure), null)
at call_user_func_array(object(Closure), [object(Request), object(Closure), null]) in Middleware.php line 119
at Middleware->think\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Middleware.php line 74
at Middleware->dispatch(object(Request)) in App.php line 399
at App->run() in index.php line 21

经过排查,发现这个问题只用修改如下地方:

// 是否自动转换URL中的控制器和操作名
    'url_convert'            => false,

把值设置为false即可,然后再刷新浏览器,发现可以得到如下打印:

Hello Dude

其实这里就是一个简单的框架使用问题,我在这里特意记录一下,希望能有所帮助。

猜你喜欢

转载自blog.csdn.net/y505772146/article/details/80068964