Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper syntax for Laravel 5.1 Artisan Command

I'm trying to make an artisan command for Laravel 5.1 and I'm getting stuck getting even the most basic version to work.

Steps taken

1) php artisan make:console Zelda --command=zelda

2) file created in app/Console/Command/Zelda.php with the following contents

<?php

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

class Zelda extends Command
{
    protected $signature = 'zelda';
    protected $description = 'Command description.';
    public function __construct() {
        parent::__construct();
    }
    public function handle() {}
}

3) The autoloader in composer.json looks like this

"autoload": {
        "classmap": [
            "database",
            "app/Console/Commands"
        ],
        "psr-4": {
            "App\\": "app/"
        }
    },
"autoload-dev": {
        "classmap": [
            "tests/TestCase.php"
        ]
},

4) then I run php artisan list and no zelda

What am I missing here?

like image 289
jeanpier_re Avatar asked Dec 31 '25 23:12

jeanpier_re


1 Answers

You're almost there! You've done everything you need to do to create your command class. However, you still need to tell your application that it should use this command class.

That is, in your CLI application kernel file (as opposed to your web application's kernel) file you should see the following

#File: app/Console/Kernel.php
class Kernel extends ConsoleKernel
{
    //...    
    protected $commands = [
        \App\Console\Commands\Inspire::class,
    ];
    //...
}

Once you've created you command (either manually or via make:console, you still need add your new command class (the full PHP class name) to the $commands array of the kernel file.

This

protected $commands = [
    \App\Console\Commands\Inspire::class,
    '\App\Console\Commands\Zelda',
];

or this

protected $commands = [
    \App\Console\Commands\Inspire::class,
    \App\Console\Commands\Zelda::class,
];

should get you where you need to be (I'm uncertain why Laravel 5.1's core code uses the magic constant class here)

like image 145
Alan Storm Avatar answered Jan 02 '26 14:01

Alan Storm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!