Defining console commands via closure in Laravel 5.3
This is a series of posts on New Features in Laravel 5.3.
!Warning: This post is over a year old. I don't always update old posts with new information, so some of this information may be out of date.
Before Laravel 5.3, defining an Artisan console command—something like php artisan sync:dates
—required you to create a new class for that command and register it in the Console Kernel. This is fine, but sometimes it feels like overkill for what might end up just being a single line of functional code.
As of Laravel 5.3, you'll notice that there's a new method in the Console/Kernel.php
file named commands()
, and it loads a new file at routes/console.php
. This new "console routes" file allows us to define Artisan console commands with a single Closure instead the prior "define a class then register it in the console Kernel" flow. Much faster, much easier.
So, open up routes/console.php
and you'll already see a sample command:
Artisan::command('inspire', function () {
$this->comment(Inspiring::quote());
})->describe('Display an inspiring quote');
As you can see, we have a new fluent builder for defining Artisan commands. We've got the signature
("inspire"), the handle()
(the closure), and the description
("Display an inspiring quote").
What if we have a parameter, or if we want to inject a dependency? It works just like it did with the old syntax.
Artisan::command('sync:conference {id}', function (JoindIn $joindin) {
$joindin->syncConference($this->argument('id'));
})->describe('Sync a given conference from JoindIn');
But here's something else interesting you can do that you can't with traditional Artisan command definition: you can take your signature arguments as parameters in the Closure, which is much more like what you'd expect if you were new to Laravel:
Artisan::command('sync:conference {id}', function ($id, JoindIn $joindin) {
$joindin->syncConference($id);
})->describe('Sync a given conference from JoindIn');
As you can see, we now have a simpler, more convenient, more fluent, and more compact way to define Artisan commands. Boom.
Comments? I'm @stauffermatt on Twitter
Tags: laravel • laravel 5.3 • artisan
This is part of a series of posts on New Features in Laravel 5.3:
-
Jun 16, 2016 | laravel, laravel 5.3, echo, websockets
-
Jun 27, 2016 | laravel, laravel 5.3
-
Jun 29, 2016 | laravel, laravel 5.3, eloquent
-
Jul 6, 2016 | laravel, laravel 5.3
-
Jul 8, 2016 | laravel, laravel 5.3
-
Jul 8, 2016 | laravel, laravel 5.3, eloquent
-
Jul 25, 2016 | laravel, laravel 5.3
-
Jul 26, 2016 | laravel, laravel 5.3
-
Jul 27, 2016 | laravel, laravel 5.3, routing
-
Jul 27, 2016 | laravel, laravel 5.3, laravel scout, laravel passport, mailable
-
Jul 29, 2016 | laravel, laravel 5.3, laravel scout
-
Jul 30, 2016 | laravel, laravel 5.3, laravel passport, oauth
-
Aug 5, 2016 | laravel, laravel 5.3, mail, laravel mailables
-
Aug 8, 2016 | laravel, laravel 5.3
-
Oct 19, 2016 | laravel, laravel 5.3, laravel notifications, notifications
-
Dec 21, 2016 | laravel, laravel 5.3, vuejs, vueify, authorization
-
Dec 21, 2016 | laravel, laravel 5.3, queues
-
Jan 30, 2017 | laravel, laravel 5.3, artisan