【发布时间】:2015-12-14 18:27:17
【问题描述】:
我收到错误:Class App\Http\Controllers\TranslatorService does not exist,无论控制器中的命名空间是否正确设置以及文件位于正确的位置。
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');
});
});
});
app/ComdexxSolutions/Http/Controllers下的AccountController.php是标准的骨架控制器。
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;
}
}
目录结构:
vagrant@homestead:~/Code/comdexx-solutions-dcas$ tree -L 2 app 应用程序 ├── ComdexxSolutions │ ├── 计费 │ ├── 控制台 │ ├── 合约 │ ├── DbCustomer.php │ ├── 活动 │ ├── 例外 │ ├── 外墙 │ ├── Http │ ├── InvokeUser.php │ ├── 招聘信息 │ ├── 听众 │ ├── 型号 │ ├── 提供者 │ ├── 仓库 │ ├── 搜索 │ ├── 服务 │ ├── 规格 │ ├── 特质 │ ├── 变形金刚 │ └── 浏览量 ├── 控制台 │ ├── 命令 │ └── Kernel.php ├── 实体 ├── 活动 │ └── Event.php ├── 例外 │ └── Handler.php ├── Http │ ├── 控制器 │ ├── Kernel.php │ ├── 中间件 │ ├── 请求 │ └── routes.php ├── 招聘信息 │ └── Job.php ├── 听众 ├── 模块 ├── 提供者 │ ├── AppServiceProvider.php │ ├── ErrorServiceProvider.php │ ├── EventServiceProvider.php │ └── RouteServiceProvider.php ├── 仓库 └── 用户.php 33 个目录,13 个文件【问题讨论】:
-
您可以试试这个:
php artisan dump-autoload或composer dump-autoload。如果这不起作用,则TranslatorService类可能需要扩展controller -
您说您的文件名为 TranslationService.php,但您的类是 TranslatorService。您是否尝试过将文件重命名为与类同名?
-
我想主要是在 composer.json 中没有更新新类时发生。如果您确定使用的命名空间是正确的,请检查此答案HERE。