Magiranu
Magiranu

Reputation: 309

Slim 3 Class not found (Controller)

I'm currently trying to get familiar with Slim 3 and just want to add a simple controller.

Error message:

Details
Type: Error
Message: Class 'app\controllers\HomeController' not found
File: C:\xampp\htdocs\slim\app\config\dependencies.php
Line: 13

My project structure:

\app
   \config
      routes.php
      dependencies.php
   \controllers
      HomeController.php
\public
   index.php
composer.json

composer.json

"autoload": {
    "psr-4" : {
        "App\\" : "app/"            
    }
}

dependencies.php

<?php
$container = $app->getContainer();

// controller
$container['HomeController'] = function($container) {
    return new app\controllers\HomeController;
};

routes.php

<?php

$app->get('/', 'HomeController:index');

HomeController.php

<?php

namespace App\Controllers;

class HomeController 
{
    public function index()
    { ... }
}

index.php

<?php    
require __DIR__ . '/../vendor/autoload.php';  
require __DIR__ . '/../app/config/settings.php';
$app = new \Slim\App(["settings" => $config]);    
require __DIR__ . '/../app/config/dependencies.php';    
require __DIR__ . '/../app/config/routes.php'; 
$app->run();

What I tried else:

I would appreciate any suggestions from you!

Upvotes: 2

Views: 4465

Answers (2)

Richard Dayo Ali
Richard Dayo Ali

Reputation: 1

Don't know who this might help. So we had a similar issue.

What we did was to remove the vendor file and then reinstall the dependencies with "composer install".

Upvotes: 0

Federkun
Federkun

Reputation: 36989

PHP's namespaces is case insensitive, Windows file system is case insensitive, but AFAIK composer's autoloader is not. Try with:

"autoload": {
    "psr-4" : {
        "app\\" : "app/"            
    }
}

Upvotes: 4

Related Questions