Php 获得;“类不存在”;在Laravel 5.x中
我得到一个错误:Php 获得;“类不存在”;在Laravel 5.x中,php,laravel,Php,Laravel,我得到一个错误:Class-App\Http\Controllers\TranslatorService不存在,无论名称空间在控制器中设置是否正确,文件是否位于正确位置 route.php: Route::group(['prefix' => 'api', 'middleware' => 'response-time'], function () { Route::group(['prefix' => 'v1'], function () { R
Class-App\Http\Controllers\TranslatorService不存在
,无论名称空间在控制器中设置是否正确,文件是否位于正确位置
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/comdexsolutions/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;
}
}
只是对这一点的快速跟进-我在IRC上帮助了这个用户,最后我们做了一个webex。根本原因是与上面发布的文件完全不同的文件
在controller.php中调用了TranslatorService
,但控制器找不到正确的命名空间。因此出现了错误
查找起来有点困难,因为错误没有标记为来自controller.php
发布问题的用户最终在整个项目中对TranslatorService进行了全局搜索,我们查看了找到问题的每个文件,直到找到问题
如果您阅读本文是因为您有类似的错误,请记住以下提示:
类不存在
-通常表示您试图使用代码中找不到的内容。它可能拼写错误,但更常见的是,它是名称空间的问题
如果你有这个错误——搜索所有东西的技术是一个很好的方法,如果它不是很明显的话
他的comdexsolutions
是app
文件夹,我想你可以试试这个:php-artisan-dump-autoload
或composer-dump-autoload
。如果这不起作用,可能类TranslatorService
需要扩展controller
您说过您的文件名为TranslationService.php,但您的类是TranslatorService。您是否尝试过将文件重命名为与类相同的名称?我想主要是在composer.json中没有更新新类时发生的。如果您确定使用的名称空间是正确的,请检查此答案。
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