0% found this document useful (0 votes)
55 views2 pages

Actions and Filters in Laravel (Eventy)

The document discusses actions and filters in Laravel using the Eventy library. Actions are used to log events, defined by calling \Eventy::action with an action name and parameters. Corresponding action listeners are defined in the Actions folder and listen for action invocations. Filters allow modifying passed data, defined by calling \Eventy::filter and returning altered data from listener functions in the Filters folder. The AppServiceProvider is configured to load the action and filter files.

Uploaded by

ali raza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views2 pages

Actions and Filters in Laravel (Eventy)

The document discusses actions and filters in Laravel using the Eventy library. Actions are used to log events, defined by calling \Eventy::action with an action name and parameters. Corresponding action listeners are defined in the Actions folder and listen for action invocations. Filters allow modifying passed data, defined by calling \Eventy::filter and returning altered data from listener functions in the Filters folder. The AppServiceProvider is configured to load the action and filter files.

Uploaded by

ali raza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Actions and Filters in Laravel

In example below actions working will be illustrated while logging history of a user critical actions.

Actions
To log history, actions will be used, whenever it is desired to log a message in history, one can define an
action using:

\Eventy::action('user_created', $user);

Above line defines it is using “Eventy” to invoke an action named/tagged “user_created” and “$user”
(newly created user) is passed as parameter.

On the other hand we have to define actions which will listen to the above action invoker. For this, to
keep logic clean and separate two new folders introduced into “app” directory named “Actions” and
“Filters”. Actions folder will contain all of the actions, in this folder each module will have its own class
which will be defining the event listener end as given below:
class UserActions {

private $history_slug = 'User';

private $obj_hashids = '';

public function __construct() {

$this->obj_hashids = new \Hashids\Hashids(config('app.key'));

\Eventy::addAction('user_created', function( $user ) {

history()->withType($this->history_slug)

->withEntity($user->id)

->withText('trans("history.backend.users.created") <strong>{user}</strong>')

->withIcon('plus')

->withClass('bg-green')

->withIP()

->withAssets([

'user_link' => ['admin.access.user.show', $user->name, $this->obj_hashids->encode( $user->id )],

])

->log();

}, 20, 1);

new UserActions();
History slug will be defined for each module to distinct history log for each module and this needs to be
added to “history_types” table, hash_ids are used to encrypt any IDs used.

Then Action listener is defined which will listen to the respective Action invoker and receive any passed
data and execute any task assigned with in anonymous function. (In this case we will be logging history.)

Now both ends got defined, let’s tell laravel about listeners to be got loaded when service providers
loaded so that whenever an Action invoked its Listener should be in memory. So following code is added
to “AppServiceProvider”
includeRouteFiles(base_path() . '/app/Actions/');

includeRouteFiles(base_path() . '/app/Filters/');

Above lines need to added in each package as every package will be having its own Actions and Filters.

Filters
For filters most of the things are same as above, the main difference is “filters return data”.

Like Actions, Filters also have two ends one calling and other listening end. On calling end we pass some
data and then on the other end that data can be altered. Following line of code will create a filter allow
to modify passed data and return it back:
$str = “Hello”;

$str = Eventy::filter('message_for_user', $str);

Whereas on the other end a class will be created inside “app/Filters” folder containing filters for each
module.
class UserFilters {

private $history_slug = 'User';

public function __construct() {

\Eventy::addFilter('message_for_user', function( $str ) {

return “Hi”;

}, 20, 1);

new UserFilters();

Now above filter listener will change string to “Hi”, we can hook in anywhere from within the application
scope.

For further details on API of Eventy:

https://github.com/tormjens/eventy

You might also like