简体   繁体   中英

Where do I put event listeners and handlers?

I am wondering where to put the Laravel Event Listeners and Handlers. Somebody told me that I can put them anywhere. This is what I have tried so far.

# listeners/log.php
<?php
Event::listen('log.create', 'LogHandler@create');

# handlers/LogHandler.php
<?php
class LogHandler {
        public function create(){
           $character = new Character;
           $character->name = "test";
           $character->save();
    }
}

# controllers/MainController.php
    public function test(){
        Event::fire('log.create');
        return "fired";
     }

# start/global.php
ClassLoader::addDirectories(array(
    app_path().'/commands',
    app_path().'/controllers',
    app_path().'/models',
    app_path().'/database/seeds',
    app_path().'/libraries',
    app_path().'/listeners',
    app_path().'/handlers',
));

I'm going to assume that you're asking this because they're not working, rather than for confirmation of something that you've got working.

Whilst it is correct that you can put event listeners anywhere, you need to make sure they'll actually get included - Laravel doesn't search through your source code looking for them.

My favourite place to include such files is in start/global.php . If you look at the bottom of the file you can see where the filters are included, you can do the same to include your listeners. It would be cleanest to keep them all in one listeners file, like all of your routes are in one routes file...

# start/global.php
require app_path().'/filters.php';

My personal opinion is that it's bad practice in general to lump event listeners in a single place. Sure, today you only need 2 or 3, but scope can be added to any project at any time, possible adding a lot more.

Instead, I generally create a directory underneath the app directory (eg app/CompanyName ) and put all of my application specific code in there. To tell Laravel how to find your files, you can then update your composer.json llike this:

"autoload": {
    "classmap": [
        // ...
    ],
    "psr-4": {
        "CompanyName\\" : "app/"
    },
}

After that, be sure to run composer dump-autoload .

Now, you can create namespace directories inside of your custom application directory, like app/CompanyName/Events/ , and be able to segregate out your event listeners into groups that make sense, and put them inside of a service provider, for example:

<?php namespace CompanyName/Events;
// File: app/CompanyName/Events/LogEventsProvider.php

use Illuminate\Support\ServiceProvider;

class LogEventsProvider extends ServiceProvider
{
    public function register()
    {
        Event::listen('log.create', 'CompanyName/Events/LogEventsProvider@create');
    }

    public function create()
    { 
        // ...
    }
}

Now you can add this service provider to your app/config/app.php and be good to go, and have all of your related event listeners in a single file, and ALL of your event listeners in a single directory, yet separate so that if something goes wrong with one of them you don't have to search through ALL of them to find where the error is happening.

NB: I did not come up with this as a practice, but found it somewhere along the way. I however cannot remember where it was.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM