简体   繁体   中英

Slim3 - Container: Class not found

My source code structure :

  • src
    • controllers
      • controller.php
      • Auth
        • AuthController.php
      • User
        • UserController.php
    • models

composer.json :

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

AuthController.php:

<?php

namespace App\Controllers\Auth;

use App\Models\User;

use App\Controllers\Controller;


class AuthController extends Controller
{  
    ...
}

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:

Class 'App\\Controllers\\User\\UserController' not found

And also IntelliJ marks the code and gives notification: Undefined class UserController.

Working with AuthController works fine.

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. But in your directory structure controller is lower case. You have to capitalize controllers directory name to Controllers . Then your namespace can be usable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM