35

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

3
  • 1
    Lumen does not have builtin Commands support Commented Mar 1, 2016 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
    Commented Oct 9, 2018 at 8:36
  • checkout the answer stackoverflow.com/questions/52716203/…
    – xiarnousx
    Commented Oct 9, 2018 at 9:21

3 Answers 3

55

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.

3
  • what about using flipbox\lumen-generator:"^5.6" package?
    – xiarnousx
    Commented Oct 9, 2018 at 8:15
  • 1
    Hieu Le I used flipbox\lumen-generator checkout this answer stackoverflow.com/questions/52716203/…
    – xiarnousx
    Commented Oct 9, 2018 at 9:22
  • 1
    Hi @xiarnousx, thank you for the package you suggested. My answer was posted three years ago :)
    – Hieu Le
    Commented Oct 9, 2018 at 13:51
20

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
];
2
  • It says: ReflectionException Class App\Console\Commands\CommandName does not exits in ...Container.php:752 Commented Nov 11, 2018 at 5: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
    Commented Jan 4, 2019 at 1: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

3
  • Please write the solution, links can break.
    – Mr Mush
    Commented Nov 23, 2015 at 12:01
  • 1
    As predicted, broken link
    – darryn.ten
    Commented Jun 28, 2018 at 16:11
  • I removed the broken link and kept the solution
    – Muhammad
    Commented Jun 30, 2018 at 6:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.