在 Laravel 5.x 中获取“类不存在”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32637888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting "class does not exist" in Laravel 5.x
提问by jabbott7_depaul
I'm getting the error: Class App\Http\Controllers\TranslatorService does not exist
regardless of the namespace being set in the controller correctly and the file being in the correct location.
我收到错误消息:Class App\Http\Controllers\TranslatorService does not exist
无论在控制器中正确设置了命名空间并且文件位于正确位置。
route.php:
路线.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.php
under app/ComdexxSolutions/Http/Controllers
is a standard skeleton controller.
AccountController.php
下面app/ComdexxSolutions/Http/Controllers
是一个标准的骨架控制器。
TranslationService.php
is under the same path as AccountController
and looks like:
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;
}
}
Directory Structure:
目录结构:
vagrant@homestead:~/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
回答by basementHyman
Just a quick follow up on this one - I helped this user on IRC and we ended up doing a webex. The root cause ended up being that a totally different file than what was posted above.
只是快速跟进这个 - 我在 IRC 上帮助了这个用户,我们最终做了一个 webex。根本原因最终是一个与上面发布的完全不同的文件。
In controller.phpthere was a call to TranslatorService
, but the correct namespace wasn't present for controller to find TranslatorService. Hence the error.
在controller.php 中有一个对 的调用TranslatorService
,但控制器找不到正确的命名空间以找到TranslatorService。因此错误。
It was a little harder to find because the error didn't flag as coming from controller.php
它有点难找到,因为错误没有标记为来自 controller.php
The user who posted the question ended up doing a global search on TranslatorService across his project, and we looked at each file it was found in until we found the issue.
发布问题的用户最终在他的项目中对 TranslatorService 进行了全局搜索,我们查看了在其中找到的每个文件,直到找到问题为止。
If you're reading this because you have a similar error, a few tips to remember are:
如果您正在阅读本文是因为您遇到了类似的错误,请记住以下几点提示:
class does not exist
- usually means you're trying to use something in your code that can't be found. It might be misspelled but more often than not, it's an issue with namespaces.If you have this error - the technique of searching everything can be a great way to find where this is coming from if it's not immediately obvious.
class does not exist
- 通常意味着您正在尝试在代码中使用无法找到的内容。它可能拼写错误,但通常情况下,这是namespaces的问题。如果您遇到此错误 - 如果不是很明显,搜索所有内容的技术可能是查找错误来源的好方法。