Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lumen make:command

Tags:

php

laravel

lumen

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

like image 342
trajan Avatar asked May 15 '15 03:05

trajan


People also ask

How do you make a command in Lumen?

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.

Does lumen have artisan?

Artisan is the command-line interface included with Lumen. It provides a number of helpful commands that can assist you while you build your application.

How do I create a lumen project?

To start with, install composer and then change your directory to the root folder of the server. Create a lumen project “lumen_api” from the “laravel/lumen” package (packagist.org). The composer will create a folder “lumen_api” and install all files of lumen including dependency.


3 Answers

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.

like image 189
Hieu Le Avatar answered Oct 13 '22 14:10

Hieu Le


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
];
like image 28
chemisax Avatar answered Oct 13 '22 13:10

chemisax


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

like image 11
Muhammad Avatar answered Oct 13 '22 12:10

Muhammad