Class not found exception in a route of slim framework

php routes namespaces eloquent slim

1832 просмотра

3 ответа

81 Репутация автора

I'm starting with Slim+eloquent+twig (using this presentation ) 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:


"name": "slim/slim-skeleton",
"description": "A Slim Framework skeleton application for rapid development",
"keywords": ["microframework", "rest", "router", "psr7"],
"homepage": "",
"license": "MIT",
"authors": [{
    "name": "Josh Lockhart",
    "email": "",
    "homepage": ""
"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"


$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);


  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.


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:


Tnx a lot !!!

Автор: Biguá Источник Размещён: 22.08.2016 09:12

Ответы (3)

1 плюс

79891 Репутация автора

The problem is that Localizacao is in App\Models namespace and in routes.php you use:


without any namespace so it's assumed it's from global namespace.

You can replace it into:


or use it like before but at the top of file add:

use App\Models\Localizacao;
Автор: Marcin Nabiałek Размещён: 23.08.2016 06:52

2 плюса

2414 Репутация автора


Your composer entry for PSR-4 autoloading is incorrect

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

Should be

"psr-4": {
    "App\\Models\\": "app/models"
Автор: geggleto Размещён: 23.08.2016 12:20

0 плюса

474 Репутация автора

Let Make things Clear here, the reason why this error is because the name you use for your psr-4 is not the same with the name you declare in your controller class: for example:

 "autoload-dev": {
        "psr-4": {
            "{name you will like to use for your name space here, that will represent your application name e.g any name here (App) for instance}\\": "the directory you will like to load your asset,library, routes,middle-ware etc e.g like app or src/"
Автор: Michael Размещён: 10.07.2017 10:55
Вопросы из категории :