What is Hooks in Lara Dashboard #
Lara Dashboard uses https://github.com/tormjens/eventy this Github package to generate action-filter hook like WordPress did for Laravel. We’ve then used some wrapper methods to use those anywhere in the code easily like a PHP function.
What is Action Hook
Actions are pieces of code you want to execute at certain points in your code. Actions never return anything but merely serve as the option to hook in to your existing code without having to mess things up.
What is Filter Hook
Filters are made to modify entities. They always return some kind of value. By default they return their first parameter and you should too.
Why we’ve used Action-filter hook #
As Lara Dashboard is a module based project, means – people can create their own module easily, so it must needed a way so that people can easily do/inject anything at any place of the code. Thats why adding action-filter hook is kinda mandatory for this project.
How we’ve used #
In LaraDashboard, we’ve made some wrapper methods using eventy package. Here is that code snippet of that hooks file –
<?php
use TorMorten\Eventy\Facades\Events as Eventy;
/**
* Add action to Eventy.
*
* This is a WordPress like add_action hook implementation.
*
* @see https://github.com/tormjens/eventy
*
* @param mixed $callback
* @param mixed $priority
* @param mixed $args
* @return void
*/
function ld_add_action(string $hookName, $callback, $priority = 20, $args = 1)
{
Eventy::addAction($hookName, $callback, $priority, $args);
}
/**
* Do action in Eventy.
*
* This is a WordPress like do_action hook implementation.
*
* @see https://github.com/tormjens/eventy
*
* @param mixed $args
* @return void
*/
function ld_do_action(string $hookName, $args = null)
{
Eventy::action($hookName, $args);
}
/**
* Apply filters in Eventy.
*
* This is a WordPress like apply_filters hook implementation.
*
* @see https://github.com/tormjens/eventy
*
* @param mixed $value
* @param mixed $args
* @return mixed
*/
function ld_apply_filters(string $hookName, $value, $args = null)
{
return Eventy::filter($hookName, $value, $args);
}
/**
* Add filter to Eventy.
*
* This is a WordPress like add_filter hook implementation.
*
* @see https://github.com/tormjens/eventy
*
* @param mixed $callback
* @param mixed $priority
* @param mixed $args
* @return mixed
*/
function ld_add_filter(string $hookName, $callback, $priority = 20, $args = 1)
{
return Eventy::addFilter($hookName, $callback, $priority, $args);
}
So, here are the method details –
ld_do_action()
: Trigger actions.ld_add_action()
: Attach callbacks to actions.ld_apply_filters()
: Apply filters to modify values.ld_add_filter()
: Attach callbacks to filters.
Examples how to use those methods – #
ld_do_action()
#
This function triggers an action hook, allowing all callbacks attached to the specified hook to execute.
Purpose: To execute all actions attached to a specific hook.
Parameters:
string $hookName
: The name of the action hook.
mixed $args
: (Optional) Arguments to pass to the callbacks. Default isnull
.
Returns: void
Example:
<?php ld_do_action('my_custom_action', ['arg1', 'arg2']);
ld_add_action()
#
This function attaches a callback to a specific action hook.
Purpose: To register a callback to be executed when the specified action hook is triggered.
Parameters:
string $hookName
: The name of the action hook.
callable $callback
: The callback function to execute.
int $priority
: (Optional) The priority of the callback. Lower numbers execute earlier. Default is20
.
int $args
: (Optional) The number of arguments the callback accepts. Default is1
.
Returns: void
Example:
ld_add_action('my_custom_action', function ($arg1, $arg2) {
echo "Action triggered with arguments: $arg1, $arg2";
}, 10, 2);
ld_apply_filters()
#
This function applies all filters attached to a specific filter hook, allowing the value to be modified by the callbacks.
Purpose: To pass a value through all callbacks attached to a filter hook.
Parameters:
string $hookName
: The name of the filter hook.
mixed $value
: The value to filter.
mixed $args
: (Optional) Additional arguments to pass to the callbacks. Default isnull
.
Returns: mixed
The filtered value
Example:
<?php $filteredValue = ld_apply_filters('my_custom_filter', 'original_value');
ld_add_filter()
#
This function attaches a callback to a specific filter hook.
Purpose: To register a callback to modify a value when the specified filter hook is applied.
Parameters:
string $hookName
: The name of the filter hook.callable $callback
: The callback function to execute.int $priority
: (Optional) The priority of the callback. Lower numbers execute earlier. Default is20
.int $args
: (Optional) The number of arguments the callback accepts. Default is1
.
Returns: void
Example:
ld_add_filter('my_custom_filter', function ($value) {
return strtoupper($value);
});
Here is the list of all possible hooks for Lara Dashboard.
Sidebar & Navbar Hooks #
The following hooks are used in the sidebar menu to allow customization and extension of the menu items:
sidebar_menu_after_dashboard
Location: After the “Dashboard” menu item.
Purpose: Allows adding custom menu items or content after the “Dashboard” menu item.
Example Usage:
ld_add_action('sidebar_menu_after_dashboard', function () {
echo '<li><a href="/custom-link" class="menu-item group">Custom Link</a></li>';
});
sidebar_menu_after_roles
Location: After the “Roles & Permissions” menu section.
Purpose: Allows adding custom menu items or content after the “Roles & Permissions” section.
Example Usage:
ld_add_action('sidebar_menu_after_roles', function () {
echo '<li><a href="/custom-roles" class="menu-item group">Custom Roles</a></li>';
});
sidebar_menu_after_users
Location: After the “Users” menu section.
Purpose: Allows adding custom menu items or content after the “Users” section.
Example Usage:
ld_add_action('sidebar_menu_after_users', function () {
echo '<li><a href="/custom-users" class="menu-item group">Custom Users</a></li>';
});
sidebar_menu_after_modules
Location: After the “Modules” menu item.
Purpose: Allows adding custom menu items or content after the “Modules” menu item.
Example Usage:
ld_add_action('sidebar_menu_after_modules', function () {
echo '<li><a href="/custom-modules" class="menu-item group">Custom Modules</a></li>';
});
sidebar_menu_after_monitoring
Location: After the “Monitoring” menu section.
Purpose: Allows adding custom menu items or content after the “Monitoring” section.
Example Usage:
ld_add_action('sidebar_menu_after_monitoring', function () {
echo '<li><a href="/custom-monitoring" class="menu-item group">Custom Monitoring</a></li>';
});
Header Hooks #
The following hooks are used in the header to allow customization and extension of the header content:
dark_mode_toggler_before_button
Location: Before the dark mode toggle button.
Purpose: Allows adding custom content or buttons before the dark mode toggle button.
Example Usage:
ld_add_action('dark_mode_toggler_before_button', function () {
echo '<button class="custom-button">Custom Button</button>';
});
dark_mode_toggler_after_button
Location: After the dark mode toggle button.
Purpose: Allows adding custom content or buttons after the dark mode toggle button.
Example Usage:
ld_add_action('dark_mode_toggler_after_button', function () {
echo '<span class="custom-text">Custom Text</span>';
});
Summary of Hooks #
Hook Name | Location | Purpose |
---|---|---|
sidebar_menu_after_dashboard | After “Dashboard” menu item | Add custom items after “Dashboard”. |
sidebar_menu_after_roles | After “Roles & Permissions” section | Add custom items after “Roles & Permissions”. |
sidebar_menu_after_users | After “Users” section | Add custom items after “Users”. |
sidebar_menu_after_modules | After “Modules” menu item | Add custom items after “Modules”. |
sidebar_menu_after_monitoring | After “Monitoring” section | Add custom items after “Monitoring”. |
dark_mode_toggler_before_button | Before dark mode toggle button | Add custom content before the toggle button. |
dark_mode_toggler_after_button | After dark mode toggle button | Add custom content after the toggle button. |
These hooks provide flexibility to extend and customize the sidebar menu and header without modifying the core code.
User Form Hooks #
Hooks for User Pages #
user_create_page_before
#
- Location: Triggered before rendering the “Create User” page.
- Purpose: Allows adding custom logic or content before the “Create User” page is displayed.
- Example Usage:<?phpld_add_action(‘user_create_page_before’, function () { echo ‘<p>Custom content before the Create User page.</p>’;});
user_edit_page_before
#
- Location: Triggered before rendering the “Edit User” page.
- Purpose: Allows adding custom logic or content before the “Edit User” page is displayed.
- Example Usage:<?phpld_add_action(‘user_edit_page_before’, function () { echo ‘<p>Custom content before the Edit User page.</p>’;});
user_edit_page_before_with_user
#
- Location: Triggered before rendering the “Edit User” page, with the user object passed as a parameter.
- Purpose: Allows modifying the user object before it is passed to the view.
- Parameters:
$user
: The user object being edited.
- Example Usage:<?phpld_add_filter(‘user_edit_page_before_with_user’, function ($user) { $user->name = strtoupper($user->name); return $user;});
Hooks for User Actions #
user_store_before_save
#
- Location: Triggered before saving a new user to the database.
- Purpose: Allows modifying the user object before it is saved.
- Parameters:
$user
: The user object being created.$request
: The request object containing form data.
- Example Usage:<?phpld_add_filter(‘user_store_before_save’, function ($user, $request) { $user->username = strtolower($user->username); return $user;}, 10, 2);
user_store_after_save
#
- Location: Triggered after saving a new user to the database.
- Purpose: Allows performing additional actions after the user is saved.
- Parameters:
$user
: The user object that was created.$request
: The request object containing form data.
- Example Usage:<?phpld_add_action(‘user_store_after_save’, function ($user, $request) { // Send a welcome email Mail::to($user->email)->send(new WelcomeEmail($user));}, 10, 2);
user_store_after
#
- Location: Triggered after the entire “Create User” process is completed.
- Purpose: Allows performing additional actions after the user creation process.
- Parameters:
$user
: The user object that was created.
- Example Usage:<?phpld_add_action(‘user_store_after’, function ($user) { Log::info(‘New user created: ‘ . $user->name);});
user_update_before_save
#
- Location: Triggered before saving an updated user to the database.
- Purpose: Allows modifying the user object before it is saved.
- Parameters:
$user
: The user object being updated.$request
: The request object containing form data.
- Example Usage:<?phpld_add_filter(‘user_update_before_save’, function ($user, $request) { $user->email = strtolower($user->email); return $user;}, 10, 2);
user_update_after_save
#
- Location: Triggered after saving an updated user to the database.
- Purpose: Allows performing additional actions after the user is updated.
- Parameters:
$user
: The user object that was updated.$request
: The request object containing form data.
- Example Usage:<?phpld_add_action(‘user_update_after_save’, function ($user, $request) { Log::info(‘User updated: ‘ . $user->name);}, 10, 2);
user_update_after
#
- Location: Triggered after the entire “Update User” process is completed.
- Purpose: Allows performing additional actions after the user update process.
- Parameters:
$user
: The user object that was updated.
- Example Usage:<?phpld_add_action(‘user_update_after’, function ($user) { Log::info(‘User update process completed for: ‘ . $user->name);});
user_delete_before
#
- Location: Triggered before deleting a user from the database.
- Purpose: Allows modifying the user object or performing actions before deletion.
- Parameters:
$user
: The user object being deleted.
- Example Usage:<?phpld_add_filter(‘user_delete_before’, function ($user) { Log::info(‘Preparing to delete user: ‘ . $user->name); return $user;});
user_delete_after
#
- Location: Triggered after deleting a user from the database.
- Purpose: Allows performing additional actions after the user is deleted.
- Parameters:
$user
: The user object that was deleted.
- Example Usage:<?phpld_add_action(‘user_delete_after’, function ($user) { Log::info(‘User deleted: ‘ . $user->name);});
Hooks for User Profile #
user_profile_update_data_before
#
- Location: Triggered before updating the authenticated user’s profile.
- Purpose: Allows modifying the profile data before it is saved.
- Parameters:
$data
: The profile data being updated.$user
: The authenticated user object.
- Example Usage:<?phpld_add_filter(‘user_profile_update_data_before’, function ($data, $user) { $data[‘name’] = strtoupper($data[‘name’]); return $data;}, 10, 2);
user_profile_update_after
#
- Location: Triggered after updating the authenticated user’s profile.
- Purpose: Allows performing additional actions after the profile is updated.
- Parameters:
$user
: The authenticated user object.
- Example Usage:<?phpld_add_action(‘user_profile_update_after’, function ($user) { Log::info(‘Profile updated for user: ‘ . $user->name);});
Hooks for User List Page #
user_list_page_table_header_before_action
#
- Location: Before the “Action” column in the user list table header.
- Purpose: Allows adding custom table headers before the “Action” column.
- Example Usage:<?phpld_add_action(‘user_list_page_table_header_before_action’, function () { echo ‘<th>Custom Header</th>’;});
user_list_page_table_header_after_action
#
- Location: After the “Action” column in the user list table header.
- Purpose: Allows adding custom table headers after the “Action” column.
- Example Usage:<?phpld_add_action(‘user_list_page_table_header_after_action’, function () { echo ‘<th>Another Custom Header</th>’;});
user_list_page_table_row_before_action
#
- Location: Before the “Action” column in the user list table rows.
- Purpose: Allows adding custom table row content before the “Action” column.
- Parameters:
$user
: The user object for the current row.
- Example Usage:<?phpld_add_action(‘user_list_page_table_row_before_action’, function ($user) { echo ‘<td>’ . $user->created_at->format(‘Y-m-d’) . ‘</td>’;});
user_list_page_table_row_after_action
#
- Location: After the “Action” column in the user list table rows.
- Purpose: Allows adding custom table row content after the “Action” column.
- Parameters:
$user
: The user object for the current row.
- Example Usage:<?phpld_add_action(‘user_list_page_table_row_after_action’, function ($user) { echo ‘<td>Custom Content</td>’;});
user_list_page_avatar_item
#
- Location: Modifies the avatar URL for the user list page.
- Purpose: Allows customizing the avatar URL for each user.
- Parameters:
$avatar
: The default avatar URL.$user
: The user object.
- Example Usage:<?phpld_add_filter(‘user_list_page_avatar_item’, function ($avatar, $user) { return $user->custom_avatar_url ?? $avatar;}, 10, 2);
Summary of Hooks #
Hook Name | Purpose |
---|---|
user_create_page_before | Add custom logic/content before the “Create User” page. |
user_edit_page_before | Add custom logic/content before the “Edit User” page. |
user_edit_page_before_with_user | Modify the user object before rendering the “Edit User” page. |
user_store_before_save | Modify the user object before saving a new user. |
user_store_after_save | Perform actions after saving a new user. |
user_store_after | Perform actions after the entire “Create User” process. |
user_update_before_save | Modify the user object before saving an updated user. |
user_update_after_save | Perform actions after saving an updated user. |
user_update_after | Perform actions after the entire “Update User” process. |
user_delete_before | Modify the user object or perform actions before deletion. |
user_delete_after | Perform actions after deleting a user. |
user_profile_update_data_before | Modify profile data before saving. |
user_profile_update_after | Perform actions after updating the profile. |
user_list_page_table_header_before_action | Add custom table headers before the “Action” column. |
user_list_page_table_header_after_action | Add custom table headers after the “Action” column. |
user_list_page_table_row_before_action | Add custom table row content before the “Action” column. |
user_list_page_table_row_after_action | Add custom table row content after the “Action” column. |
user_list_page_avatar_item | Customize the avatar URL for the user list page. |
These hooks provide flexibility to customize and extend the user management functionality.