Search code examples
phpslimpsr-4

Can't load class on Slim 3


I'm trying to load my custom classes for the model on Slim 3 (using the skeleton) so I made this:

In app/composer.json:

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

In routes.php I have this setting:

<?php

use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Container;


// Routes
$app->get('/sugiere', function (Request $request, Response $response, array $args) {
    // Sample log message
    $this->logger->info("Slim-Skeleton '/' route");
    $cat_mapper = new \App\Classes\CategoryMapper($this->db);
    $comuna_mapper = new \App\Classes\ComunaMapper($this->db);
    $lang_mapper = new \App\Classes\LanguageMapper($this->db);
    $netw_mapper = new \App\Classes\NetworkMapper($this->db);
    $com_list = $com_mapper->getComunaList();
    $cat_list = $cat_mapper->getCategoryList();
    $lang_list = $lang_mapper->getLangList();
    $netw_list = $netw_mapper->getNetworkList();

By the way I added to all classes a namespace App\Classes on top.


Solution

  • Your path /src/classes looks incorrect. It's unlikely your src directory is in the filesystem root.

    Change your composer.json file to

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

    and run

    composer dump-autoload
    

    to re-generate the autoload.php file.

    See https://getcomposer.org/doc/01-basic-usage.md#autoloading