Laravel 5 make:controller 在 app 文件夹而不是控制器文件夹中创建控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26117753/
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
Laravel 5 make:controller creating controller in app folder instead of controller folder
提问by user300979
I'm learning how to use Laravel 5 and I've ran into a problem where my controllers are being created in the root of the "app" folder instead of the "controller" folder. I have no idea why this is happening as I've re installed and checked 10 times.
我正在学习如何使用 Laravel 5,但我遇到了一个问题,我的控制器是在“app”文件夹的根目录中创建的,而不是“控制器”文件夹。我不知道为什么会发生这种情况,因为我已经重新安装并检查了 10 次。
I'm in gitbash on windows 8.1..
我在 windows 8.1 上的 gitbash ..
So I go
所以我去
john@John ~/desktop/code/my-first-app
$ php artisan make:controller PagesController
and then I get
然后我得到
Controller created successfully
控制器创建成功
Only it's being created in the root of app
and nothing in the Controllers folder. What am I missing? I also see others having the same problem in comments under video on laracasts.
只有它是在根目录中创建的,app
而在 Controllers 文件夹中没有任何内容。我错过了什么?我也在laracasts视频下的评论中看到其他人有同样的问题。
回答by Vijay Sebastian
In Laravel 5, it is not required to specify the path. By default, it will generate the controller in the directory.
在 Laravel 5 中,不需要指定路径。默认情况下,它会在目录中生成控制器。
So, the controller can be created like this:
因此,可以像这样创建控制器:
php artisan make:controller controllerName
However, if you would like to create it in a custom directory then refer to the line below:
但是,如果您想在自定义目录中创建它,请参考以下行:
php artisan make:controller pathName/controllerName
回答by Matthew Way
After trying php artisan make:controller Directory\PageController
, Laravel 5.1 would create a controller named DirectoryPage Controller in my app directory. The solution for me was to escape the backslash with another backslash so the following worked for me:
尝试后php artisan make:controller Directory\PageController
,Laravel 5.1 将在我的应用程序目录中创建一个名为 DirectoryPage Controller 的控制器。我的解决方案是用另一个反斜杠转义反斜杠,所以以下对我有用:
php artisan make:controller Directory\\PageController
Laravel created a Pagecontroller in the app/Directory
. Just thought I would share with everyone.
Laravel 在app/Directory
. 只是想我会与大家分享。
回答by Surjit Sidhu
The controller can be created into specific path as followed:
可以将控制器创建到特定路径中,如下所示:
php artisan make:controller controllerName
However, if you would like to create it in a custom directory then refer to the line below:
但是,如果您想在自定义目录中创建它,请参考以下行:
below will create on root path (outside app directory)
下面将在根路径(应用程序目录外)上创建
php artisan make:controller App\../pathName/controllerName
If want inside app directory then it should be
如果想要在 app 目录中,那么它应该是
php artisan make:controller App\pathName/controllerName
Tested on Laravel 6.x
在 Laravel 6.x 上测试
回答by Jitesh Lakhwani
php artisan make:controller -r controllerName
php artisan make:controller -r controllerName
Try it with -r option it will create controller with boiler plate (All the basic function like: - public function index(){}, public function store(Request $request){}, and so on...... for better understanding you can also visit: - https://laracasts.com/series/laravel-from-scratch-2017
尝试使用 -r 选项,它将创建带有样板的控制器(所有基本功能,如:- public function index(){}、public function store(Request $request){} 等等......对于更好的理解你也可以访问: - https://laracasts.com/series/laravel-from-scratch-2017
回答by Marcin Nabia?ek
It seems there is a bug in creating controllers.
创建控制器时似乎存在错误。
I've checked it a minute ago using Laravel 5 I've installed about week or two and it was working fine. But when I installed fresh Laravel 5 now controllers are created in app/
folder and not in app/Http/Controllers
.
我在一分钟前使用 Laravel 5 检查过它,我已经安装了大约一两周,它运行良好。但是当我安装新的 Laravel 5 时,现在控制器是在app/
文件夹中创建的,而不是在app/Http/Controllers
.
But it's also possible that developers changed their mind where controllers will be stored (Laravel 5 is still in development).
但也有可能开发人员改变了控制器的存储位置(Laravel 5 仍在开发中)。
At the moment nobody knows that, you could also look at Laracast discussionabout this issue.
目前没人知道,你也可以看看Laracast关于这个问题的讨论。
What is also strange you cannot manually add path to this command.
还有什么奇怪的,你不能手动添加路径到这个命令。
In Laravel 4 you can do:
在 Laravel 4 中,您可以执行以下操作:
php artisan controller:make --path="app/Controllers" PagesController
but in Laravel5 for controller:make
you have less options comparing to Laravel 4, they are:
但是在 Laravel5 中,controller:make
与 Laravel 4 相比,您的选择较少,它们是:
Options:
--plain Generate an empty controller class.
--help (-h) Display this help message.
--quiet (-q) Do not output any message.
--verbose (-v|vv|vvv) Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
--version (-V) Display this application version.
--ansi Force ANSI output.
--no-ansi Disable ANSI output.
--no-interaction (-n) Do not ask any interactive question.
--env The environment the command should run under.
so you cannot put here custom path or don't generate some methods as you can in Laravel 4.
所以你不能把自定义路径放在这里,也不能像在 Laravel 4 中那样生成一些方法。
EDIT
编辑
It seems it is working again as it should in the newest Laravel 5 commit. So you should either create new Laravel 5 project or run:
它似乎在最新的 Laravel 5 提交中再次正常工作。因此,您应该创建新的 Laravel 5 项目或运行:
composer update
to update project.
更新项目。
However you will probably get some errors as for example:
但是,您可能会遇到一些错误,例如:
{"error":{"type":"Symfony\Component\Debug\Exception\FatalErrorException","message":"Class 'Illuminate\Routing\FilterServiceProvider' not found","file":
{"error":{"type":"Symfony\Component\Debug\Exception\FatalErrorException","message":"找不到类 'Illuminate\Routing\FilterServiceProvider'","file":
to remove those errors:
删除这些错误:
in file app/Providers/FilterServiceProvider.php
change
在文件app/Providers/FilterServiceProvider.php
更改中
use Illuminate\Routing\FilterServiceProvider as ServiceProvider;
into:
进入:
use Illuminate\Foundation\Support\Providers\FilterServiceProvider as ServiceProvider;
and in file in file app/Providers/RouteServiceProvider.php
change
和文件中的文件app/Providers/RouteServiceProvider.php
更改
use Illuminate\Routing\RouteServiceProvider as ServiceProvider;
into:
进入:
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
回答by JeffreyWay
Do a composer update, and it'll be back to normal.
做一个作曲家更新,它会恢复正常。