Jan 26, 2015 | laravel, laravel 5

Laravel 5.0 - Generating Missing Events

Series

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

!
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.

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:

app/
    Events/
        Event.php
    Handlers/
        Events/

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 => [
            RespondOneWay::class,
            RespondAnotherWay::class
        ]
    ];

2) Run php artisan event:generate

3) Profit.

Check it out.

app/
    Events/
        Event.php
        DidSomethingEvent.php
    Handlers/
        Events/
            RespondOneWay.php
            RespondAnotherWay.php

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


This is part of a series of posts on New Features in Laravel 5.0:

  1. Sep 10, 2014 | laravel, 5.0, laravel 5
  2. Sep 10, 2014 | laravel, 5.0, laravel 5
  3. Sep 12, 2014 | laravel, laravel 5, 5.0
  4. Sep 20, 2014 | laravel, 5.0, laravel 5
  5. Sep 28, 2014 | laravel, laravel 5, 5.0
  6. Sep 30, 2014 | laravel, 5.0, laravel 5
  7. Oct 9, 2014 | laravel, 5.0, laravel 5
  8. Oct 10, 2014 | laravel, 5.0, laravel 5
  9. Oct 10, 2014 | laravel, 5.0, laravel 5
  10. Nov 20, 2014 | laravel, 5.0, laravel 5
  11. Jan 2, 2015 | laravel, 5.0, commands, laravel 5
  12. Jan 16, 2015 | laravel, laravel 5
  13. Jan 19, 2015 | laravel 5, laravel
  14. Jan 21, 2015 | laravel, events, 5.0, laravel 5
  15. Jan 26, 2015 | laravel, laravel 5
  16. Feb 1, 2015 | laravel, laravel 5
  17. Feb 14, 2015 | laravel 5, laravel, eloquent

Subscribe

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