Laravel 5.0 - Generating Missing Events

(This is part of a series of posts on New Features in Laravel 5.0.)

  1. Laravel 5.0 - Form Requests
  2. Laravel 5.0 - ValidatesWhenResolved
  3. Laravel 5.0 - Directory structure and namespace
  4. Laravel 5.0 - Route Caching
  5. Laravel 5.0 - Cloud File Drivers
  6. Laravel 5.0 - Method Injection
  7. Laravel 5.0 - Route Annotations (removed)
  8. Laravel 5.0 - Event Annotations (removed)
  9. Laravel 5.0 - Middleware (Filter-style)
  10. Laravel 5.0 - Environment Detection & Environment Variables
  11. Laravel 5.0 - Event Scheduling
  12. Laravel 5.0 - Commands & Handlers
  13. Upgrading from Laravel 4 to Laravel 5
  14. Bringing Whoops Back to Laravel 5
  15. Laravel 5.0 - Events & Handlers
  16. Laravel 5.0 - Generating Missing Events
  17. Laravel 5.0 - Custom Error Pages
  18. Laravel 5.0 - Eloquent Attribute Casting

Sometimes it can seem like a lot of work to create an event, create its handler, and bind the two. Create a command, create its handler, bind the two. I've often wished for a workflow that handled the whole process together in one.

The artisan commands for generating commands and events are a good start--they both create their own entity and (optionally) its handler. But you still can spend an hour writing the command and handler, and then waste another 15 minutes trying to figure out why it's not working, only to realize you never actually bound the two together.

Enter event:generate #

Well, dear reader, your white-knuckled wait is finally over. In Laravel 5, you can bind (non-existent) events and handlers in the EventServiceProvider, run php artisan event:generate, and Artisan will automatically generate the files for you--both for the Event and its Handler.

Make it happen #

Check out our events and handlers directories before:


1) Open app/providers/EventServiceProvider.php. Find the $listen property, which is where you would normally bind your events, and add one in the following format:

    protected $listen = [
        DidSomethingEvent::class => [

2) Run php artisan event:generate

3) Profit.

Check it out.


Created. Bound. Ready to go. Even typehinted:

<?php namespace App\Handlers\Events;
class RespondOneWay {
    public function handle(DidSomethingEvent $event)

That was easy, right? #

Yah, that's it. You can now design your eventing system abstractly--you could plan the entire thing without writing a single command or handler. And once you're ready to go, generate all of your events and handlers in a single command.

Comments? I'm @stauffermatt on Twitter

Tags: laravel | laravel 5

Matt Stauffer headshot

Hi, I'm Matt Stauffer.

I'm partner & technical director at Tighten Co.

You can find me on Twitter at @stauffermatt

Like what you're reading?

I wrote an entire 450+ page book for O'Reilly: Laravel: Up and Running.

You can order the eBook or print book today.