我正在尝试通过命令行在我的Lumen安装中执行代码。在完整的Laravel中,我读到可以使用“make:command”命令来实现此目的,但是Lumen似乎不支持此命令。
有没有办法启用此命令?如果没有,最佳的在Lumen中从CLI运行代码的方式是什么?
谢谢
flipbox\lumen-generator
,请查看这个答案 https://stackoverflow.com/questions/52716203/laravel-lumen-framework5-7-and-flipbox-lumen-generator5-6-class-not-fou/52717071#52717071 - xiarnousx这是一个新命令的模板。您可以将其复制并粘贴到新文件中开始工作。我在lumen 5.7.0上测试过它。
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class CommandName extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'commandSignature';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->info('hello world.');
}
}
然后在Kernel.php文件中注册它。
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
\App\Console\Commands\CommandName::class
];
app/Console/Kernel.php
中正确指定类,则会看到该错误。我刚刚提交了一份编辑答案,使用更好的 ::class
表示法,如果该类不存在,大多数IDE都会对您进行突出显示(如果是字符串则不会)。 - alexkb当你创建命令类时,请使用以下内容:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
不要像上面描述的那样使用serve command
示例,而是……
flipbox\lumen-generator
的包,但我在bootstrap\app.php
中注册这个包时遇到了困难。可能是因为它与lumen:5.7.*
不兼容。 - xiarnousx