Jan 30, 2017 | laravel, laravel 5.3, artisan

Defining console commands via closure in Laravel 5.3

Series

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:

  1. Jun 16, 2016 | laravel, laravel 5.3, echo, websockets
  2. Jun 27, 2016 | laravel, laravel 5.3
  3. Jun 29, 2016 | laravel, laravel 5.3, eloquent
  4. Jul 6, 2016 | laravel, laravel 5.3
  5. Jul 8, 2016 | laravel, laravel 5.3, eloquent
  6. Jul 25, 2016 | laravel, laravel 5.3
  7. Jul 26, 2016 | laravel, laravel 5.3
  8. Jul 27, 2016 | laravel, laravel 5.3, routing
  9. Jul 27, 2016 | laravel, laravel 5.3, laravel scout, laravel passport, mailable
  10. Jul 29, 2016 | laravel, laravel 5.3, laravel scout
  11. Jul 30, 2016 | laravel, laravel 5.3, laravel passport, oauth
  12. Aug 5, 2016 | laravel, laravel 5.3, mail, laravel mailables
  13. Aug 8, 2016 | laravel, laravel 5.3
  14. Oct 19, 2016 | laravel, laravel 5.3, laravel notifications, notifications
  15. Dec 21, 2016 | laravel, laravel 5.3, vuejs, vueify, authorization
  16. Dec 21, 2016 | laravel, laravel 5.3, queues
  17. Jan 30, 2017 | laravel, laravel 5.3, artisan

Subscribe

For quick links to fresh content, and for more thoughts that don't make it to the blog.