php 流明制造:命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30251065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:39:29  来源:igfitidea点击:

Lumen make:command

phplaravellumen

提问by trajan

I'm trying to execute code within my Lumen install via the command line. In full Laravel , I've read that you can use commands to achieve this via "make:command", but Lumen does not seem to support this command.

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

Is there anyway to enable this command? Failing that, what's the best way of running code from the CLI in Lumen?

无论如何要启用此命令?否则,从 Lumen 的 CLI 运行代码的最佳方式是什么?

Thanks

谢谢

回答by Hieu Le

You can use the artisanCLI in Lumen as the same way as in Laravel but with fewer built-in commands. To see all built-in commands, use the php artisancommand in Lumen.

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

Although there is no make:commandcommand at Lumen, you can create your custom command:

尽管make:commandLumen没有命令,但您可以创建自定义命令:

  • Add new command class inside the app/Console/Commandsfolder, you can use the sample class template of the framework servecommand

  • Register your custom command by adding your created class to the $commandsmember inside the app/Console/Kernel.phpfile.

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

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

Except the command generating, you can use the Laravel docsfor commands when working with Lumen.

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

回答by Muhammad

When you create your command class use this:

创建命令类时,请使用以下命令:

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

Instead of what was described above about using serve commandexample

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

回答by chemisax

Here is a template for a new command. You can just copy and paste this in to a new file and start working. I tested it on lumen 5.7.0

这是一个新命令的模板。您只需将其复制并粘贴到新文件中即可开始工作。我在流明 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.');
    }
}

Then register it on the Kernel.php file.

然后在 Kernel.php 文件中注册它。

/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
   \App\Console\Commands\CommandName::class
];