我正在尝试通过命令行在我的 Lumen 安装中执行代码。在完整的 Laravel 中,我读到你可以通过“make:command”使用命令来实现这一点,但 Lumen 似乎不支持这个命令。
反正有没有启用这个命令?如果做不到这一点,在 Lumen 中从 CLI 运行代码的最佳方式是什么?
谢谢
您可以artisan
像在 Laravel 中一样使用 Lumen 中的 CLI,但内置命令更少。要查看所有内置命令,请使用php artisan
Lumen 中的命令。
虽然 Lumen 没有make:command
命令,但您可以创建自定义命令:
在文件夹内添加新的命令类app/Console/Commands
,可以使用框架serve
命令的示例类模板
通过将您创建的类添加到文件中的$commands
成员来注册您的自定义命令app/Console/Kernel.php
。
除了命令生成之外,您可以在使用 Lumen 时使用Laravel 文档来获取命令。
这是一个新命令的模板。您可以将其复制并粘贴到新文件中并开始工作。我在 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
];
当您创建命令类时,请使用:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
而不是上面关于使用serve command
示例的描述