2015-09-17 20 views
1

,我發現了錯誤:Class App\Http\Controllers\TranslatorService does not exist而不考慮命名空間正在控制器正確設置和文件在正確的位置是的。獲得「類不存在」在Laravel 5.x的

route.php:

Route::group(['prefix' => 'api', 'middleware' => 'response-time'], function () { 
    Route::group(['prefix' => 'v1'], function() { 
     Route::get('/', function() { 
      App::abort(404); 
     }); 

     Route::resource('accounts', 'AccountController'); 
    }); 

    Route::group(['prefix' => 'v2'], function() { 
     Route::get('/', function() { 
      App::abort(501, 'Feature not implemented'); 
     }); 
    }); 
}); 

AccountController.phpapp/ComdexxSolutions/Http/Controllers下是一個標準的骨架控制器。

TranslationService.php相同路徑下爲AccountController,看起來像:

<?php 

namespace ComdexxSolutions\Http\Controllers; 

use InvalidArgumentException; 
use RuntimeException; 
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; 

class TranslatorService 
{ 
    /** 
    * Returns a class name base on a resource mapping. 
    * The mapping comes from a config file (api.php). 
    * 
    * Example: `users` should return `\App\Models\User` 
    * 
    * @param string $resource 
    * @return string 
    * @throws NotFoundHttpException 
    */ 
    public function getClassFromResource($resource) 
    { 
     // This is the models namespace 
     $modelsNamespace = Config::get('api.models_namespace',  Config::get('api.app_namespace')); 

     // This array contains mapping between resources and Model classes 
     $mapping = Config::get('api.mapping'); 

     if (!is_array($mapping)) { 
      throw new RuntimeException('The config api.mapping needs to be an array.');  
     } 

     if (!isset($mapping[$resource])) { 
      throw new NotFoundHttpException; 
     } 

     return implode('\\', [$modelsNamespace, $mapping[$resource]]); 
    } 

    /** 
    * Returns a command class name based on a resource mapping. 
    * 
    * Examples: 
    *  - getCommandFromResource('users', 'show') returns  \App\Commands\UserCommand\ShowCommand 
    *  - getCommandFromResource('users', 'index', 'groups') returns  \App\Commands\UserCommand\GroupIndexCommand 
    * 
    * @param string $resource 
    * @param string $action 
    * @param string|null $relation 
    * @return string 
    * @throws NotFoundHttpException 
    */ 
    public function getCommandFromResource($resource, $action, $relation = null) 
    { 
     $namespace = Config::get('api.app_namespace'); 
     $mapping = Config::get('api.mapping'); 

     // If the mapping does not exist, we consider this as a 404 error 
     if (!isset($mapping[$resource])) { 
      throw new NotFoundHttpException('The resource [' . $resource . '] is not mapped to any Model.'); 
     } 

     $allowedActions = ['index', 'store', 'show', 'update', 'destroy']; 

     if (!in_array($action, $allowedActions)) { 
      throw new InvalidArgumentException('[' . $action . '] is not a valid action.'); 
     } 

     // If we have a $relation parameter, then we generate a command based on it 
     if ($relation) { 
      if (!isset($mapping[$relation])) { 
       throw new NotFoundHttpException('The resource [' . $resource . '] is not mapped to any Model.'); 
      } 

      $command = implode('\\', [ 
       $namespace, 
       'Commands', 
       $mapping[$resource] . 'Command', 
       ucfirst($mapping[$relation]) . ucfirst($action) . 'Command' 
      ]); 
     } else { 
      $command = implode('\\', [ 
       $namespace, 
       'Commands', 
       $mapping[$resource] . 'Command', 
       ucfirst($action) . 'Command' 
      ]); 
     } 

     // If no custom command is found, then we use one of the default ones 
     if (!class_exists($command)) { 
      if ($relation) { 
       $command = implode('\\', [ 
        'ComdexxSolutions', 
        'Console', 
        'Commands', 
        'DefaultCommand', 
        'Relation' . ucfirst($action) . 'Command' 
       ]); 
      } else { 
       $command = implode('\\', [ 
        'ComdexxSolutions', 
        'Console', 
        'Commands', 
        'DefaultCommand', 
        ucfirst($action) . 'Command' 
        ]); 
        } 
       } 

     if (!class_exists($command)) { 
      throw new NotFoundHttpException('There is no default command for this action and resource.'); 
     } 

     return $command; 
    } 
} 

目錄結構:

http://laravel.io/bin/l59qa

 
[email protected]:~/Code/comdexx-solutions-dcas$ tree -L 2 app 
app 
├── ComdexxSolutions 
│ ├── Billing 
│ ├── Console 
│ ├── Contracts 
│ ├── DbCustomer.php 
│ ├── Events 
│ ├── Exceptions 
│ ├── Facades 
│ ├── Http 
│ ├── InvokeUser.php 
│ ├── Jobs 
│ ├── Listeners 
│ ├── Models 
│ ├── Providers 
│ ├── Repositories 
│ ├── Search 
│ ├── Services 
│ ├── spec 
│ ├── Traits 
│ ├── Transformers 
│ └── Views 
├── Console 
│ ├── Commands 
│ └── Kernel.php 
├── Entities 
├── Events 
│ └── Event.php 
├── Exceptions 
│ └── Handler.php 
├── Http 
│ ├── Controllers 
│ ├── Kernel.php 
│ ├── Middleware 
│ ├── Requests 
│ └── routes.php 
├── Jobs 
│ └── Job.php 
├── Listeners 
├── Modules 
├── Providers 
│ ├── AppServiceProvider.php 
│ ├── ErrorServiceProvider.php 
│ ├── EventServiceProvider.php 
│ └── RouteServiceProvider.php 
├── Repositories 
└── User.php 

33 directories, 13 files 
+0

你可以試試這個:'php artisan dump-autoload'或'composer dump-autoload'。如果還是不行,也許TranslatorService'需要擴展的類''controller' – mauricehofman

+0

你說你的文件被命名爲TranslationService.php但你的類是TranslatorService。您是否嘗試將您的文件重命名爲與課程名稱相同的名稱? – Zsw

+0

大多會發生在新的類不獲得composer.json更新我想。如果您確信使用的命名空間是正確的,然後檢查這個答案[*** *** HERE(http://stackoverflow.com/questions/32475892/reflectionexception-class-classname-does-not-exist-laravel/32476236#32476236)。 –

回答

1

只是快速跟進這個 - 我幫助這個IRC上的用戶,我們最終做了一個webex。根本原因最終成爲一個完全不同於上面發佈的文件。

controller.php有一個電話TranslatorService,但控制器沒有找到正確的命名空間來查找TranslatorService。因此錯誤。

這是一個有點難以找到,因爲錯誤沒有標誌從Controller.php這樣

誰張貼問題的用戶終於實現了橫跨他的項目TranslatorService全局搜索來了,我們看着直到找到問題才找到每個文件。

如果你正在讀這篇文章,因爲你有一個類似的錯誤,一些提示要牢記:

  1. class does not exist - 通常意味着你要使用的東西在你的代碼不能找到。它可能拼錯,但往往不是,它與命名空間的問題。

  2. 如果你有這樣的錯誤 - 搜索一切都可以是一個偉大的方式來找到在哪裏,這是從,如果它不是立即明顯未來的技術。

+0

他的'ComdexxSolutions'是我認爲的'app'文件夾 – mauricehofman