soroushrah/action

There is no license information available for the latest version (dev-master) of this package.

Action and Event like Wordpress Hooks

dev-master 2021-06-13 10:04 UTC

This package is auto-updated.

Last update: 2025-07-13 18:50:32 UTC


README

eventy logo


Hook in Laravel

About

Actions are pieces of code you want to execute at certain points in your code.

Installation

  1. Install using Composer
composer require soroushrah/action
  1. Add the service provider to the providers array in your config/app.php.
    Soroush\Action\ActionServiceProvider::class,
    Soroush\Action\ActionBladeServiceProvider::class,
  1. Add the facade in config/app.php
    'Action' => \Soroush\Action\Facades\Action::class,

Usage

Actions

Anywhere in your code you can create a new action like so:

\Action::action('my.hook', 'soroush');

The first parameter is the name of the hook And Everything You pass after is argument.

To listen to your hooks, 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:

\Action::addAction('my.hook', function($what) {
    echo 'Hello '. $what;
}, 20, 1);

Again the first argument must be the name of the hook. second would be a callback. Third is Priority. Fourth is number of arguments would accepted.(default is 1)

Using in Blade

Adding the same action as the one in the action example above:

@action('my.hook', 'awesome')

Add Daynamic Relation Models

For add some relation on models are not in your module you should use HasDynamicRelation trait in your all project model :

class MyModel extends Model {
    use HasDynamicRelation;
}

Then add dynamic relation like example above on providers :

MyModel::addDynamicRelation('some_relation', function(MyModel $model) {
    return $model->hasMany(SomeRelatedModel::class);
});

Credits

  • Created by Soroush Rahmani