Jurid
Jurid

Reputation: 35

Slim3 - Container: Class not found

My source code structure:

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 

Upvotes: 1

Views: 1566

Answers (1)

Shaharia Azam
Shaharia Azam

Reputation: 1978

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.

Upvotes: 1

Related Questions