[英]Slim3 - Container: Class not found
My source code structure : 我的源代码结构 :
composer.json : composer.json :
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
AuthController.php: AuthController.php:
<?php
namespace App\Controllers\Auth;
use App\Models\User;
use App\Controllers\Controller;
class AuthController extends Controller
{
...
}
UserController.php: UserController.php:
<?php
namespace App\Controllers\User;
use App\Models\User;
use App\Controllers\Controller;
class UserController extends Controller
{
...
}
in Slim app: 在苗条的应用程序:
$container = $app->getContainer();
$container['AuthController'] = function ($container){
return new App\Controllers\Auth\AuthController($container);
};
$container['UserController'] = function ($container){
return new App\Controllers\User\UserController($container);
};
I get an error when calling function from UserController: 从UserController调用函数时出错:
Class 'App\\Controllers\\User\\UserController' not found 未找到“App \\ Controllers \\ User \\ UserController”类
And also IntelliJ marks the code and gives notification: Undefined class UserController. 并且IntelliJ标记代码并给出通知:未定义的类UserController。
Working with AuthController works fine. 使用AuthController工作正常。
In my opinion I got wrong namespacing, but I am not sure how to change it. 在我看来,我得到了错误的命名空间,但我不知道如何改变它。
I have also run: 我也跑了:
composer dump-autoload -o
You are using Controllers
as the namespace class. 您正在使用Controllers
作为命名空间类。 But in your directory structure controller
is lower case. 但是在你的目录结构中, controller
是小写的。 You have to capitalize controllers
directory name to Controllers
. 您必须将controllers
目录名称大Controllers
。 Then your namespace can be usable. 然后您的命名空间可以使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.