Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Support shorter subscription syntax #38408

Merged
merged 1 commit into from
Aug 16, 2021
Merged

Conversation

taylorotwell
Copy link
Member

@taylorotwell taylorotwell commented Aug 16, 2021

This PR allows you to return an array from the subscribe method (this was already supported) that has a shorter syntax for methods that exist on the subscriber itself:

<?php

namespace App\Listeners;

use App\Events\SomeEvent;

class SomeEventSubscriber
{
    /**
     * Handle an event.
     */
    public function handleSomeEvent($event)
    {
        info('Handling some event...');
    }

    /**
     * Register the listeners for the subscriber.
     *
     * @param  \Illuminate\Events\Dispatcher  $events
     * @return void
     */
    public function subscribe($events)
    {
        return [
            SomeEvent::class => [
                'handleSomeEvent',
            ],
        ];

        // Or...

        return [
            SomeEvent::class => 'handleSomeEvent',
        ];
    }
}

@binaryweavers
Copy link

binaryweavers commented Aug 16, 2021

I am not sure if I am on right path (correct me please if I am missing something). I was managing the other way around. Having listeners of an event be on itself. Did that for code management purpose so that by looking at an event I would know what are it's listeners 🤔
Doing something this in EventServiceProvider.php
image

@GrahamCampbell GrahamCampbell changed the title Support shorter subscription syntax [8.x] Support shorter subscription syntax Aug 16, 2021
@taylorotwell taylorotwell merged commit 9bfe672 into 8.x Aug 16, 2021
@taylorotwell taylorotwell deleted the shorter-subscriptions branch August 16, 2021 21:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants