【问题标题】:Lumen make:command流明制造:命令
【发布时间】:2015-07-26 21:07:19
【问题描述】:

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

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

谢谢

【问题讨论】:

  • Lumen 没有内置命令支持
  • 有一个包flipbox\lumen-generator 但是我很难在bootstrap\app.php 中注册这个包。可能是因为不兼容lumen:5.7.*

标签: php laravel lumen


【解决方案1】:

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

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

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

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

在使用 Lumen 时,除了命令生成之外,您还可以使用 Laravel docs 进行命令。

【讨论】:

  • 使用flipbox\lumen-generator:"^5.6" 包怎么样?
  • Hieu Le 我用过flipbox\lumen-generator 查看这个答案stackoverflow.com/questions/52716203/…
  • 嗨@xiarnousx,谢谢你推荐的包。我的答案是三年前发布的 :)
【解决方案2】:

这是一个新命令的模板。 您可以将其复制并粘贴到新文件中并开始工作。 我在 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
];

【讨论】:

  • 它说:ReflectionException Class App\Console\Commands\CommandName does not exits in ...Container.php:752
  • @user1633272 - 如果您没有在app/Console/Kernel.php 中正确指定您的类,您会看到该错误。我刚刚提交了对答案的编辑,以使用更好的做法::class nottion,如果类不存在,大多数 IDE 都会向您突出显示(如果它是字符串,它们不会突出显示)。
【解决方案3】:

当你创建你的命令类时,使用这个:

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

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

【讨论】:

  • 请写出解决方案,链接会断。
  • 正如预测的那样,链接断开
  • 我删除了损坏的链接并保留了解决方案
猜你喜欢
  • 2016-07-04
  • 2016-12-30
  • 1970-01-01
  • 2017-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-30
  • 1970-01-01
相关资源
最近更新 更多