Git Product home page Git Product logo

laravel-hooks's Introduction

Laravel Hooks

The WordPress filters, actions system in Laravel.

About

  • 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.

Installation

composer require millat/laravel-hooks

DONE! Now you can use laravel-hooks

Functions

do_action(  string $tag,  mixed $arg )

This function invokes all functions attached to action hook $tag. It is possible to create new action hooks by simply calling this function, specifying the name of the new hook using the $tag parameter.

Parameters

  • $tag (string) (Required) The name of the action to be executed.

  • $arg (mixed) (Optional) Additional arguments which are passed on to the functions hooked to the action. Default empty.

add_action( string $tag, callable $callback, int $priority = 10, int $accepted_args = 1 )

Actions are the hooks that the Laravel application launches at specific points during execution, or when specific events occur.

Parameters

  • $tag (string) (Required) The name of the action to add the callback to.

  • $callback (callable) (Required) The callback to be run when the action is called.

  • $priority (int) (Optional) Used to 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

  • $accepted_args (int) (Optional) The number of arguments the function accepts. Default value: 1

apply_filters( string $tag, mixed $value )

This function invokes all functions attached to filter hook $tag. It is possible to create new filter hooks by simply calling this function, specifying the name of the new hook using the $tag parameter.

Parameters

  • $tag (string) (Required) The name of the filter hook.

  • $value (mixed) (Required) The value to filter.

add_filter( string $tag, callable $callback, int $priority = 10, int $accepted_args = 1 )

Filter hooks to allow modify various types of internal data at runtime.

Parameters

  • $tag (string) (Required) The name of the filter to add the callback to.

  • $callback (callable) (Required) The callback to be run when the filter is applied.

  • $priority (int) (Optional) Used to specify the order in which the functions associated with a particular filter 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 filter. Default value: 10

  • $accepted_args (int) (Optional) The number of arguments the function accepts. Default value: 1

How to pass callback function?

The callback function could be a string referring to a class in the application MyNamespace\Http\Listener@myHookListener, an array callback [$object, 'method'] or a globally registered function global_function, anonymous function.

Example using anonymous function:

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

Example using referring to a class's method:

add_action('user_created', 'MyNamespace\Http\MyClass@myMethod', 20, 1);

Example using array callback:

add_action('user_created', [$object, 'myMethod'], 20, 1);

Usage

Actions

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

do_action('user_created', $user);

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);

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

Filters always have to have data coming in and data going out to ensure the data is output in the browser (your content may be passed through other filters before getting output in the browser). By comparison, actions, which are similar to filters, do not require that anything to be returned, although data can be returned through actions as well.

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 of how filter used in a real application.

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());
    }
}

Using filter we can modify this query:

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

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');
        });
    }
}

License

The MIT License (MIT). Please see License File for more information.

laravel-hooks's People

Contributors

sneherador avatar ahmillat avatar muhammadzihad avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.