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

Difference between Actions & Filters in Examples #27

Closed
dancameron opened this issue Sep 4, 2020 · 2 comments · Fixed by #28
Closed

Difference between Actions & Filters in Examples #27

dancameron opened this issue Sep 4, 2020 · 2 comments · Fixed by #28

Comments

@dancameron
Copy link
Contributor

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="'.$post->url.'">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);
        });
    }
}
@tormjens
Copy link
Owner

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. :)

@dancameron
Copy link
Contributor Author

💯

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 a pull request may close this issue.

2 participants