32

我正在尝试通过命令行在我的 Lumen 安装中执行代码。在完整的 Laravel 中,我读到你可以通过“make:command”使用命令来实现这一点,但 Lumen 似乎不支持这个命令。

反正有没有启用这个命令?如果做不到这一点,在 Lumen 中从 CLI 运行代码的最佳方式是什么?

谢谢

4

3 回答 3

51

您可以artisan像在 Laravel 中一样使用 Lumen 中的 CLI,但内置命令更少。要查看所有内置命令,请使用php artisanLumen 中的命令。

虽然 Lumen 没有make:command命令,但您可以创建自定义命令:

  • 在文件夹内添加新的命令类app/Console/Commands,可以使用框架serve命令的示例类模板

  • 通过将您创建的类添加到文件中的$commands成员来注册您的自定义命令app/Console/Kernel.php

除了命令生成之外,您可以在使用 Lumen 时使用Laravel 文档来获取命令。

于 2015-05-15T04:14:53.563 回答
16

这是一个新命令的模板。您可以将其复制并粘贴到新文件中并开始工作。我在 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
];
于 2018-10-07T18:31:02.527 回答
11

当您创建命令类时,请使用:

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;

而不是上面关于使用serve command示例的描述

于 2015-11-23T11:39:57.767 回答