Slim应用程序错误:设置Controller slim 3

时间:2019-07-12 08:04:35

标签: php slim

我正在设置我的HomeController,我在Macbook上。我不确定为什么会一直找不到错误类:

Slim Application Error
The application could not run because of the following error:

Details
Type: Error
Message: Class 'App\Controllers\HomeController' not found
File: /Users/username/Documents/webapps/site/src/dependencies.php
Line: 39
Trace

这是我的控制人:

<?php

namespace \App\Controllers;

class HomeController {
    protected $container;

    // constructor receives container instance
    public function __construct(ContainerInterface $container) {
        $this->container = $container;
    }

    public function index($request, $response){
        return 'home Controller';
    }

    public function videos($request, $response){
        return 'Video Controller';
    }
}

和Composer.json

"autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    },

路线:

$app->get('/videos', \HomeController::class . ':videos');

Anddepends.php

 $container['HomeController'] = function ($container) {
        return new \App\Controllers\HomeController;
    };

我认为一切都会顺利进行。 我想念什么?

1 个答案:

答案 0 :(得分:0)

确保您在路由器中正确调用了类名-解析器将尝试从Container加载实例,根据您的示例,这是错误的 应该是

$app->get('/videos', 'HomeController:videos'); // because class \HomeController::class does not exists an it is named just 'HomeController' in your container

在MacOS上也要注意,名称空间必须遵循文件夹结构,并且文件夹结构区分大小写,因此,如果您的Controllers文件夹不使用大写C,则类加载器可能会出现问题