How do I add a Laravel command in a subfolder?

时间:2015-07-31 19:25:35

标签: php laravel-5

So I'm trying to create a custom console command in Laravel 5.1 which does some helpful function for my project. I can do this fine when putting the console command in a file located at the base commands folder but not when I just to add a subdirectory.

'App\Console\Commands\SomeCustomCommandThatWorks',
'App\Console\Commands\MySubNameSpace\CustomCommandThatFails',

So how do I add my command like MySubNameSpace\Command?

Namespace doesn't appear to have any effect on this. The namespace of the command could be App\Console\MySubNameSpace\MyCommand or App\Console\MyCommand both fail if the file is located at 'App\Console\Commands\MySubNameSpace\MyCommand'. The file also fails if located at 'App\Console\Commands\MyCommand' with namespace App\Console\MySubNameSpace\MyCommand.

Right now I get this error.

Class App\Console\Commands\DeletePhantomServers does not exist

I have tried running composer dumpautoload but to no success.

Any help would be appreciated. enter image description here

2 个答案:

答案 0 :(得分:2)

根据自动加载标准,您上面没有任何组合可以使用。需要根据目录设置命名空间。所以如果你在这个文件夹中有命令

Console\Commands\MySubNameSpace\MyCommand

你的命名空间必须是

App\Console\Commands\MySubNameSpace\MyCommand

答案 1 :(得分:0)

我一定很累。在阅读了Michael的回答之后,我查看了命名空间并意识到我只是简单地写错了。

我正在把

namespace App\Console\Commands\MySubNameSpace\MyCommand;

class MyCommand ...

我需要放

的地方
namespace App\Console\Commands\MySubNameSpace;

class MyCommand ...

感谢帮助人们:)