首页 文章

流明制造:命令

提问于
浏览
19

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

反正有没有启用此命令?如果做不到这一点,从流明的CLI运行代码的最佳方法是什么?

谢谢

3 回答

  • 36

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

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

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

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

    除了生成命令之外,在使用Lumen时可以使用Laravel docs for命令 .

  • 8

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

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

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

  • 1

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

相关问题