guyaloni
guyaloni

Reputation: 5912

Laravel custom artisan command not listed

I have several artisan commands which I wrote.
All of them share common functionality, so instead of extending Command class, I wrote a MyBaseCommand class so all commands extend this one:

namespace App\Console\Commands;
use Illuminate\Console\Command;

class SomeCommand extends MyBaseCommand
{
    protected $signature = 'mycommands:command1';

    protected $description = 'Some description';

    :
    :

And the base class:

namespace App\Console\Commands;

class MyBaseCommand extends Command
{
    :
    :

The problem is that from some reason these commands are no longer listed with php artisan.

Any idea how can I force laravel to list these commands as well?

Upvotes: 1

Views: 2340

Answers (3)

guyaloni
guyaloni

Reputation: 5912

It is quite stupid, anyway since it might happen to someone else I leave here the answer:

I wanted to hide the base class, so I had inside it this line:

protected $hidden = true;

Of-course, the value of this variable was propagated to the high-level class, what made the custom commands hidden.

The solution is simply to add to these files this line:

protected $hidden = false;

====================== UPDATE ======================

As @aken-roberts mentions, a better solution is simply making the base class abstract:

namespace App\Console\Commands;

abstract class MyBaseCommand extends Command
{

    abstract public function handle();

    :
    :

In this case artisan doesn't list it, and it cannot be executed.

Upvotes: 0

oreopot
oreopot

Reputation: 3450

protected $signature = 'mycommands:command1'; //this is your command name

Open app\Console\kernel.php file.

protected $commands = [
    \App\Console\Commands\SomeCommand::class,
]

then run

php artisan list

Upvotes: 3

Kevin Pimentel
Kevin Pimentel

Reputation: 1916

Laravel tries to register the commands for you automatically with:

/**
 * Register the commands for the application.
 *
 * @return void
 */
protected function commands()
{
    $this->load(__DIR__.'/Commands');

    require base_path('routes/console.php');
}

You can find this in the App\Console\Kernel.php

Make sure that your classes have a signature and description property.

enter image description here

enter image description here

Upvotes: 0

Related Questions