34

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.

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

Thanks

trajan
  • 1,093
  • 2
  • 12
  • 15
  • 1
    Lumen does not have builtin Commands support – user1016265 Mar 01 '16 at 14:10
  • There is a package `flipbox\lumen-generator` however I am having hard time registering this package inside `bootstrap\app.php`. Probably it's because not compatible with `lumen:5.7.*` – xiarnousx Oct 09 '18 at 08:36
  • checkout the answer https://stackoverflow.com/questions/52716203/laravel-lumen-framework5-7-and-flipbox-lumen-generator5-6-class-not-fou/52717071#52717071 – xiarnousx Oct 09 '18 at 09:21

3 Answers3

53

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

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

  • Add new command class inside the app/Console/Commands folder, you can use the sample class template of the framework serve command

  • Register your custom command by adding your created class to the $commands member inside the app/Console/Kernel.php file.

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

Hieu Le
  • 8,288
  • 1
  • 34
  • 55
  • what about using flipbox\lumen-generator:"^5.6" package? – xiarnousx Oct 09 '18 at 08:15
  • 1
    Hieu Le I used `flipbox\lumen-generator` checkout this answer https://stackoverflow.com/questions/52716203/laravel-lumen-framework5-7-and-flipbox-lumen-generator5-6-class-not-fou/52717071#52717071 – xiarnousx Oct 09 '18 at 09:22
  • 1
    Hi @xiarnousx, thank you for the package you suggested. My answer was posted three years ago :) – Hieu Le Oct 09 '18 at 13:51
18

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

<?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.

/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
   \App\Console\Commands\CommandName::class
];
alexkb
  • 3,216
  • 2
  • 30
  • 30
chemisax
  • 193
  • 1
  • 7
  • It says: ReflectionException Class App\Console\Commands\CommandName does not exits in ...Container.php:752 – user1633272 Nov 11 '18 at 05:16
  • @user1633272 - you'll see that error, if you haven't correctly specified your class in `app/Console/Kernel.php`. I've just submitted an edit to the answer to use the better practice `::class` nottion, which most IDE's will highlight to you if the class doesn't exist (they won't if it's a string). – alexkb Jan 04 '19 at 01:45
11

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 command example

Noah
  • 859
  • 7
  • 17
Muhammad
  • 444
  • 4
  • 9