tormjens / eventy

WordPress-like actions and filters for Laravel

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Difference between Actions & Filters in Examples

dancameron opened this issue · comments

Thank you for all your hard work on this, I'm really excited about using it in a future project.

As someone very new to Laravel I'm reluctant to open this ticket, however as a WordPress developer (for ~15 years) I feel obligated to mention the differences between "actions" and "filters" isn't demonstrated well within the examples, since they're practically the same.

I'd suggest passing variables in hooks, otherwise it's just a filter. This would help not only with differentiate the two but show the power of creating an action hook.

use TorMorten\Eventy\Facades\Events as Eventy;
Eventy::action('my.hook', $user);
Eventy::addAction('my.hook', function($user) {
    if ($user->is_awesome) {
         $this->doSomethingAwesome($user);
    }
}, 20, 1);

Especially in regards to templating.

@action('my.hook', $user)
@action('my.hook')

Maybe an example you can use for "Using it to enable extensibility".

Here's an example of an action being added to the a blade template for extensibility by plugins that can be conditionally loaded.

@foreach ($posts as $post)
    ...
    <p>{{ $post->body }}</p>
    ...
    @action('blade-posts-loop-post', $post)
@endforeach
use TorMorten\Eventy\Facades\Events as Eventy;
class SharePostsController
{
    public function boot()
    {
        Eventy::addAction('blade-posts-loop-post', function($post) {
            echo '<a href="https://github.com/tormjens/eventy/issues/'.$post->url." target="_blank" rel="nofollow'">Share this post</a>';
            printf('<a href="https://xyz.com?share='.$post->url.'">Share this post</a>');
        });
    }
}

use TorMorten\Eventy\Facades\Events as Eventy;
class CommentsPostsController
{
    public function boot()
    {
        Eventy::addAction('blade-posts-loop-post', function($post) {
            echo 'Comments: ' . count($post->comments);
        });
    }
}

Thanks for your input. I agree this could be much clearer and I'd be happy to accept a PR to update the docs to better explain the differences. :)

💯