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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With