1

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.

ffuentes
  • 1,042
  • 5
  • 16
  • 36
  • What **exactly** is the error message? – Phil Jun 05 '18 at 03:49
  • The application could not run because of the following error: Details Type: Error Message: Class 'App\Classes\CategoryMapper' not found File: /home/ubuntu/workspace/santiago/src/routes.php Line: 13 – ffuentes Jun 05 '18 at 03:50
  • Have you run `composer dump-autoload` after adding your `autoload` configuration to `composer.json`? See https://getcomposer.org/doc/01-basic-usage.md#autoloading – Phil Jun 05 '18 at 03:50

1 Answers1

2

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

Phil
  • 157,677
  • 23
  • 242
  • 245
  • I changed it but I get the same error. I get a 500 error. – ffuentes Jun 05 '18 at 04:03
  • @ffuentes changed it to _what_, exactly? Please update your question with the configuration and code you're currently using. – Phil Jun 05 '18 at 04:15
  • @ffuentes Could you also please show a screenshot of your `src/classes` directory contents and the namespace and class definition in one of your files, eg `src/classes/CategoryMapper.php` – Phil Jun 05 '18 at 04:18
  • Now I got it. I changed the route and wrote the use statement above and when calling the method. – ffuentes Jun 05 '18 at 04:21