2014-02-23 28 views
0

作爲Laravel的新手,我跟着存儲庫簡化了關於Laracasts並創建了BackendServiceProvider類。在Laravel4中ServiceProvider的問題

現在,當我想要做的

php artisan generate:controller TestController 

我得到一個錯誤:

PHP Fatal error: Class '_testic\repos\BackendServiceProvider' not found in 
/vagrant/vendor/laravel/framework/src/Illuminate/Foundation/ProviderRepository.php 
on line 158 

什麼錯呢?我該如何解決它?

我做了什麼至今:

  1. 把我的服務提供商進入config/app.php
  2. 然後將其添加到composer.jsonpsr-0

    { 
    "name": "laravel/laravel", 
    "description": "The Laravel Framework.", 
    "keywords": ["framework", "laravel"], 
    "license": "MIT", 
    "require": { 
        "laravel/framework": "4.1.*" 
    }, 
    "autoload": { 
        "classmap": [ 
         "app/commands", 
         "app/controllers", 
         "app/models", 
         "app/database/migrations", 
         "app/database/seeds", 
         "app/tests/TestCase.php" 
        ] 
    }, 
    "psr-0": { 
         "_testic": "app/" // <----- my entry 
        }, 
    "scripts": { ... 
    
  3. composer dump-autoload -o

回答

2

psr-0進入自動加載。就在classmap旁邊。

"autoload": { 
    "classmap": [ 
     "app/commands", 
     "app/controllers", 
     "app/models", 
     "app/database/migrations", 
     "app/database/seeds", 
     "app/tests/TestCase.php" 
    ], 
    "psr-0": { 
     "_testic": "app/" // <----- my entry 
    } 
} 
1

通常這些類型的問題來自命名空間,文件名或目錄結構中的畸形。

您定義的PSR-0命名空間爲:

"_testic": "app/" --> which means 'my _testic namespace is stored in the folder app/' 

而且,根據錯誤信息,命名空間的文件

_testic\repos\BackendServiceProvider 

所以,我必須假設你將使用您的其他文件中的特定文件爲:

use _testic\repos\BackendServiceProvider; 

這也意味着你必須有你的BackendServiceProvider.php文件夾中的文件

/whateverRootFoldersYouMayHave/app/_testic/repos/BackendServiceProvider.php 

如果這不是您的文件夾結構,您將收到此錯誤。

請注意,在PSR-0中,整個文件夾結構必須與您定義爲根名稱空間的文件夾結構相同。