Biguá Biguá - 3 months ago 38
PHP Question

Class not found exception in a route of slim framework

I'm starting with Slim+eloquent+twig (using this presentation http://slides.com/revul/slimframework ) and getting this error:

Type: Error
Message: Class 'Localizacao' not found
File: /Users/Bigua/Sites/salas/src/routes.php
Line: 19


I'm trying to list all from a table and pass it to my view. The command "composer update" don't returned any problem.

here my code:

Composer.json:

{
"name": "slim/slim-skeleton",
"description": "A Slim Framework skeleton application for rapid development",
"keywords": ["microframework", "rest", "router", "psr7"],
"homepage": "http://github.com/slimphp/Slim-Skeleton",
"license": "MIT",
"authors": [{
"name": "Josh Lockhart",
"email": "info@joshlockhart.com",
"homepage": "http://www.joshlockhart.com/"
}],
"require": {
"php": ">=5.5.0",
"slim/slim": "^3.1",
"monolog/monolog": "^1.17",
"illuminate/database": "~5.1",
"slim/twig-view": "^2.1"
},
"autoload": {
"psr-4": {
"App/Models\\": "app/models"
}
}


Route

$app->get('/sala/add', function ($request, $response, $args) {
$this->logger->info("Slim-Skeleton '/salas/add' route");
// Render index view
$args['locais'] = Localizacao::all();
$this->logger->info("locais" . $args);
return $this->renderer->render($response, 'add_sala.html.twig', $args);
})->setName('sala_add');


Model

<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model as Eloquent;

class Localizacao extends Eloquent {
protected $table = 'localizacao';

protected $primaryKey = 'id_local';
public $timestamps = false;
}


I had read many similar topics here and seems to me that is a problem with namespaces, but I just don't get it where its wrong. Sorry if this is a noob question.

[SOLVED]

The solution its following the two answers, from @marcin-nabiaƂek and @geggleto :

Fixed the psr-4:

"psr-4": {
"App\\Models\\": "app/models"
}


And using namespace correctly:

\App\Models\Localizacao::all()


Tnx a lot !!!

Answer

Your composer entry for PSR-4 autoloading is incorrect

"psr-4": {
    "App/Models\\": "app/models"
}

Should be

"psr-4": {
    "App\\Models\\": "app/models"
}
Comments