laravel 无法声明类控制器,因为名称已被使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40406418/
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
Cannot declare class Controller, because the name is already in use
提问by Reshad
I recently migrated a laravel 4.2 project to 5.0. So far I have completed all the necessary steps but I keep getting an error.
我最近将一个 Laravel 4.2 项目迁移到了 5.0。到目前为止,我已经完成了所有必要的步骤,但我不断收到错误消息。
Cannot declare class Controller, because the name is already in use
无法声明类控制器,因为名称已被使用
My Controller is changed as provided by laravel in the upgrade guide.
我的控制器按照 laravel 在升级指南中的提供进行了更改。
<?php
use Illuminate\Foundation\Bus\DispatchesCommands;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
abstract class Controller extends BaseController {
use DispatchesCommands, ValidatesRequests;
}
Also I have added it to the classmap directive of my composer.json.
此外,我已将其添加到我的 composer.json 的 classmap 指令中。
"autoload": {
"classmap": [
"database",
"app/Http/Controllers"
],
"psr-4": {
"App\": "app/"
}
},
I couldn't find any solution so far so if you guys know what to do it would help me out a lot :) thanks in advance!
到目前为止,我找不到任何解决方案,所以如果你们知道该怎么做,它会帮助我很多:) 在此先感谢!
回答by Eric Tucker
Remove the "app/Http/Controllers"
from your classmap.
"app/Http/Controllers"
从您的类图中删除。
Add namespace App\Http\Controllers;
above your use
blocks.
namespace App\Http\Controllers;
在您的use
块上方添加。
Then run composer dump-auto
然后运行 composer dump-auto
回答by Gordon Freeman
I'm pretty sure you have to add namespaces.
我很确定您必须添加命名空间。
Namespacing
By default, Laravel 4 applications did not utilize namespacing within your application code. So, for example, all Eloquent models and controllers simply lived in the "global" namespace. For a quicker migration, you can simply leave these classes in the global namespace in Laravel 5 as well.
命名空间
默认情况下,Laravel 4 应用程序不会在你的应用程序代码中使用命名空间。因此,例如,所有 Eloquent 模型和控制器都位于“全局”命名空间中。为了更快地迁移,您也可以简单地将这些类保留在 Laravel 5 的全局命名空间中。