2016-08-22 44 views
0

我開始搭配修身+口才+小枝(使用此演示文稿http://slides.com/revul/slimframework),並得到這個錯誤:類未發現異常小井框架的路線

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

我想列出所有從表並將其傳遞給我的觀點。命令「作曲家更新」不返回任何問題。

這裏我的代碼:

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": "[email protected]", 
    "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" 
    } 
} 

路線

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

型號

<?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; 
    } 

我在這裏看過很多類似的主題,在我看來,這是命名空間的問題,但我不明白它在哪裏。對不起,如果這是一個noob問題。

[解決]

溶液其以下的兩個答案,從@馬爾欽-nabiałek和@geggleto:

修正了PSR-4:

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

並正確使用名稱空間:

\App\Models\Localizacao::all() 

Tnx很多!!!

+0

THX通過標籤添加@marcin =) –

回答

1

爲PSR-4自動加載你的作曲家項不正確

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

應該是

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

問題是LocalizacaoApp\Models命名空間和routes.php你使用:

Localizacao::all() 

沒有任何空間中,因此它是假設它從全局命名空間的。

您可以替換成:

\App\Models\Localizacao::all() 

或使用它像以前一樣,但在文件中添加的頂部:

use App\Models\Localizacao; 
0

讓我們把事情說清楚這裏,爲什麼這個錯誤是因爲你用你的PSR-4名是不是就在你的控制器類聲明的名稱相同的原因:例如:

"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/" 
     } 
    }