Laravel action, filter hooks system like WordPress - DEV Community

Table of Contents

Laravel action, filter hooks system like WordPress – DEV Community

An action interrupts the code flow to do something, and then returns back to the normal flow without modifying anything.

A filter is used to modify something in a specific way so that the modification is then used by code later on.
Read more about filters and actions

Hooks are a way for one piece of code to interact/modify another piece of code at specific, pre-defined spots.

Wherever you want, you can create a new action in you Laravel application:

do_action('user_created', $user);
Enter fullscreen mode

Exit fullscreen mode

Here, user_created is the name of the action, which will use later when the action will be listening. And $user is the parameters, which will be found whenever you listen the action. These can be anything.

To listen to your actions, you attach listeners. These are best added to your AppServiceProvider boot() method.

For example if you wanted to hook in to the above hook, you could do:

add_action('user_created', function($user) {
    $user->sendWelcomeMail();
}, 20, 1);
Enter fullscreen mode

Exit fullscreen mode

The first argument must be the name of the action. The second would be a closures, callbacks and anonymous functions. The third specify the order in which the functions associated with a particular action are executed. Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action. Default value: 10. And fourth, the number of arguments the function accepts. Default value: 1

Filters always have to have data coming in and data going out to ensure the data is output in the browser

Basically, filters are functions that can be used in Laravel application to pass data through. They allows developers to modify the default behavior of a specific function.

Here’s an example:

Post.php is a model or class , where it builds a query to fetch all published posts

class Post extend Model
{
    public function getPublished()
    {
        return Post::where('published_at', '>', now());
    }
}
Enter fullscreen mode

Exit fullscreen mode

Using filter we can make a offer to modify this query:

class Post extend Model
{
    public function getPublished()
    {
        return apply_filters('posts_published', Post::where('published_at', '>', now());
    }
}
Enter fullscreen mode

Exit fullscreen mode

Now, in the entry point of application like any module or plugin’s you can modify this post published query.

In Module’s or Plugin’s service provider (preferably in the boot method) we’ll add a listener for the filter.

class ModuleServiceProvider extends ServiceProvider
{
    public function boot()
    {
        add_filter('posts_published', function($query) {
            return $query->where('status', 'active');
        });
    }
}
Enter fullscreen mode

Exit fullscreen mode

This content was originally published here.