From version 1.0.3 onwards, it’s now possible to modify controllers functions by implementing “hooks”. The hooks allow you to hook into controller functions without actually modifying the controller functions themselves.
Enabling hooks
You’ll need to enable hooks by setting the “enable_hooks” setting to TRUE. The original setting is found in /application/config/config.php, however it would be best to place this setting in the “config_custom.php” (/application/config/config_custom.php) config overwrite file. This way, your setting will be save when updating the application in the future.
Setting up a hook
Below you’ll see an example of a hook:
$hook['auth_index_post'][] = array(
'class' => 'MY_Auth',
'function' => 'auth_index_post',
'filename' => 'MY_Auth.php',
'filepath' => 'hooks',
'params' => ''
);
Hooks are declared in /application/config/hooks.php.
The actual hook code should be placed in the /application/hooks folder, in a file which name matches the filename specified in the hook declaration.
So, following the example declaration provided above, you would create the following file to contain your custom hook code: /application/hooks/MY_Auth.php.
Now, whenever the hook point “auth_index_post” runs, the function auth_index_post inside your MY_Auth.php file will execute.
The custom code inside your hook files
To be able to make full use of the hook functionality and get the most flexibility when extending controller functions, you will want to make sure you can access the relevant data within your hook files. To this end, you will want to make sure you retrieve the proper CodeIgniter instance in each of your hook files. You can do this using the following template for all of your hook files:
?php
defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Auth {
private $CI;
public function __construct()
{
$this->CI =& get_instance()->controller;
}
}
Using this template, you will be able to access the CodeIgniter instance by it’s local reference: $this->CI. Please note you will need to replace the class name with the correct class name as set in your hook declaration.
Let’s go ahead and add a function which is called at the hook point auth_index_post – at the end of the index() function in the /application/modules/auth/Auth.php controller just before the view gets loaded. The index() function passes $this->data to the view file.
Let’s assume you want to pass some additional or modify existing data to the view; you can achieve this by adding a function to your hook file:
?php
defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Auth {
private $CI;
public function __construct()
{
$this->CI =& get_instance()->controller;
}
public function auth_index_post ()
{
$this->CI->data['title'] = 'Test Hooks';
}
}
With the above function added to your hook file, you’re modifying the $this->data[‘title’] attribute without messing with the original controller function.
This is of course just a very basic example. Since you have access to the CodeIgniter instance, the possibilities are limitless. You can even access the database and do data manipulation!
Available hook points
Since this functionality has only recently been introduced; we have made some estimated guesses as to which hook points to implement first. That said, the goal here is to make Bloxby as flexible as possible and if you find we are missing essential hook points, please do share your suggestions on the forums. Every requested hook point will be implemented (assuming it’s technically possible of course).
Currently, the following hook points are available:
- /application/modules/asset/controllers/Asset.php
- asset_construct
Called at the end of the __construct() function; runs when the class is first instantiated - asset_images_pre
Called at the beginning of the images() function - asset_images_post
Called at the end of the images() function - asset_imageUpload_pre
Called at the beginning of the imageUpload() function - asset_imageUpload_post
Called at the end of the imageUpload() function - asset_delImage_pre
Called at the beginning of the delImage() function - asset_delImage_post
Called at the end of the delImage() function - asset_destruct
Called in class __destruct() function
- asset_construct
- /application/modules/auth/controllers/Auth.php
- auth_construct
Called at the end of the __construct() function; runs when the class is first instantiated - auth_index_pre
Called at the beginning of the index() function - auth_index_post
Called at the end of the index() function - auth_register_pre
Called at the beginning of the register() function - auth_register_post
Called at the end of the register() function - auth_payment_stripe_pre
Called at the beginning of the payment_stripe() function - auth_payment_stripe_post
Called at the end of the payment_stripe() function - auth_payment_confirm_pre
Called the beginning of the payment_confirm() function - auth_payment_confirm_post
Called at the end of the payment_confirm() function - auth_free_user_confirm_pre
Called at the beginning of the free_user_confirm() function - auth_free_user_confirm_post
Called at the end of the free_user_confirm() function - auth_payment_card_update_pre
Called at the beginning of the payment_card_update() function - auth_payment_card_update_post
Called at the end of the payment_card_update() function - auth_activate_pre
Called at the beginning of the activate() function - auth_activate_post
Called at the end of the activate() function - auth_forgot_pre
Called at the beginning of the forgot() function - auth_forgot_post
Called at the end of the forgot() function - auth_reset_password_pre
Called at the beginning of the reset_password() function - auth_reset_password_valid_user
Called during the password reset process when a valid user has been located - auth_reset_password_invalid_user
Called during the password reset process when an invalid user has been located - auth_logout_pre
Called at the beginning of the logout() function - auth_logout_post
Called at the end of the logout() function - auth_ipn_notifier_pre
Called at the beginning of the ipn_notifier() function - auth_ipn_notifier_post
Called at the end of the ipn_notifier() function - auth_stripe_hook_pre
Called at the beginning of the stripe_hook() function - auth_stripe_hook_post
Called at the end of the stripe_hook() function - auth_destruct
Called in class __destruct() function
- auth_construct
- /application/modules/customdomain/controllers/Customdomain.php
- customdomain_construct
Called at the end of the __construct() function; runs when the class is first instantiated - customdomain_index_pre
Called at the beginning of the index() function - customdomain_index_post
Called at the end of the index() function - customdomain_index_error
Called when no page is found - customdomain_destruct
Called in class __destruct() function
- customdomain_construct
- /application/modules/package/controllers/Package.php
- package_construct
Called at the end of the __construct() function; runs when the class is first instantiated - package_index_pre
Called at the beginning of the index() function - package_index_post
Called at the end of the index() function - package_create_pre
Called at the beginning of the create() function - package_create_post
Called at the end of the create() function - package_update_pre
Called at the beginning of the update() function - package_update_post
Called at the end of the update() function - package_delete_pre
Called at the beginning of the delete() function - package_delete_post
Called at the end of the delete() function - package_toggle_status_pre
Called at the beginning of the toggle_status() function - package_toggle_status_post
Called at the end of the toggle_status() function - package_destruct
Called in class __destruct() function
- package_construct
- /application/modules/sent/controllers/Sent.php
- sent_construct
Called at the end of the __construct() function; runs when the class is first instantiated - sent_api_pre
Called at the beginning of the api() function - sent_api_post
Called at the end of the api() function - sent_msg_pre
Called at the beginning of the msg() function - sent_msg_post
Called at the end of the msg() function - sent_destruct
Called in class __destruct() function
- sent_construct
- /application/modules/settings/controllers/Settings.php
- settings_construct
Called at the end of the __construct() function; runs when the class is first instantiated - settings_index_pre
Called at the beginning of the index() function - settings_index_post
Called at the end of the index() function - settings_update_pre
Called at the beginning of the update() function - settings_update_post
Called at the end of the update() function - settings_update_payment_pre
Called at the beginning of the update_payment() function - settings_update_payment_post
Called at the end of the update_payment() function - settings_update_core_pre
Called at the beginning of the update_core() function - settings_update_core_post
Called at the end of the update_core() function - settings_destruct
Called in class __destruct() function
- settings_construct
- /application/modules/sites/controllers/Sites.php
- sites_construct
Called at the end of the __construct() function; runs when the class is first instantiated - sites_index_pre
Called at the beginning of the index() function - sites_index_post
Called at the end of the index() function - sites_create_pre
Called at the beginning of the create() function - sites_create_post
Called at the end of the create() function - sites_tsave_pre
Called at the beginning of the tsave() function - sites_tsave_post
Called at the end of the tsave() function - sites_save_pre
Called at the beginning of the save() function - sites_save_post
Called at the end of the save() function - sites_siteData_pre
Called at the beginning of the siteData() function - sites_siteData_post
Called at the end of the siteData() function - sites_site_pre
Called at the beginning of the site() function - sites_site_post
Called at the end of the site() function - sites_siteAjax_pre
Called at the beginning of the siteAjax() function - sites_siteAjax_error
Called when error is thrown in the siteAjax() function - sites_siteAjax_success
Called at the end of the siteAjax() function - sites_siteAjaxUpdate_pre
Called at the beginning of the siteAjaxUpdate() function - sites_siteAjaxUpdate_success
Called when site was updated successfully - sites_siteAjaxUpdate_error
Called when error is thrown during site updating process - sites_export_pre
Called at the beginning of the export() function - sites_export_post
Called at the end of the export() function - sites_trash_pre
Called at the beginning of the trash() function - sites_trash_post
Called at the end of the trash() function - sites_updatePageData_pre
Called at the beginning of the updatePageData() function - sites_updatePageData_post
Called at the end of the updatePageData() function - sites_livepreview_pre
Called at the beginning of the livepreview() function - sites_livepreview_post
Called at the end of the livepreview() function - sites_deltempl_pre
Called at the beginning of the deltempl() function - sites_deltempl_post
Called at the end of the livepreview() function - sites_rpreview_pre
Called at the beginning of the rpreview() function - sites_rpreview_post
Called at the end of the rpreview() function - sites_getRevisions_pre
Called at the beginning of the getRevisions() function - sites_getRevisions_post
Called at the end of the getRevisions() function - sites_deleterevision_pre
Called at the beginning of the deleterevision() function - sites_deleterevision_post
Called at the end of the getRevisions() function - sites_restorerevision_pre
Called at the beginning of the restorerevision() function - sites_restorerevision_post
Called at the end of the restorerevision() function - sites_destruct
Called in class __destruct() function
- sites_construct
- /application/modules/subdomain/controller/Subdomain.php
- subdomain_construct
Called at the end of the __construct() function; runs when the class is first instantiated - subdomain_index_pre
Called at the beginning of the index() function - subdomain_index_post
Called at the end of the index() function - subdomain_index_error
Called when no page is found - subdomain_destruct
Called in class __destruct() function
- subdomain_construct
- /application/modules/subdomain/controllers/Subfolder.php
- subfolder_construct
Called at the end of the __construct() function; runs when the class is first instantiated - subfolder_index_pre
Called at the beginning of the index() function - subfolder_index_post
Called at the end of the index() function - subfolder_index_error
Called when no page is found - subfolder_destruct
Called in class __destruct() function
- subfolder_construct
- /application/modules/subscription/controllers/Subscription.php
- subscription_construct
Called at the end of the __construct() function; runs when the class is first instantiated - subscription_paypal_pre
Called at the beginning of the paypal() function - subscription_paypal_post
Called at the end of the paypal() function - subscription_payment_confirmation_pre
Called at the beginning of the payment_confirmation() function - subscription_payment_confirmation_post
Called at the end of the payment_confirmation() function
- subscription_construct
- /application/modules/temple/controllers/Temple.php
- temple_construct
Called at the end of the __construct() function; runs when the class is first instantiated - temple_index_pre
Called at the beginning of the index() function - temple_destruct
Called in class __destruct() function
- temple_construct
- /application/modules/user/controllers/User.php
- user_construct
Called at the end of the __construct() function; runs when the class is first instantiated - user_index_pre
Called at the beginning of the index() function - user_index_post
Called at the end of the index() function - user_create_pre
Called at the beginning of the create() function - user_create_post
Called at the end of the create() function - user_update_pre
Called at the beginning of the update() function - user_update_post
Called at the end of the update() function - user_send_password_reset_pre
Called at the beginning of the send_password_reset() function - user_send_password_reset_success
Called when the function has successfully send the password reset email to the user - user_send_password_reset_error
Called when an error is thrown when trying to send the email - user_details_update_pre
Called at the beginning of the details_update() function - user_details_update_success
Called when the user’s data has successfully been updated - user_details_update_error
Called when an error was thrown while trying to update the user’s data - user_login_update_pre
Called at the beginning of the login_update() function - user_login_update_success
Called when the user’s login data was successfully updated - user_login_update_error
Called when an error is thrown while trying to update the user’s login data - user_toggle_status_pre
Called at the beginning of the toggle_status() function - user_toggle_status_post
Called at the end of the toggle_status() function - user_delete_pre
Called at the beginning of the delete() function - user_delete_post
Called at the end of the delete() function - user_package_update_pre
Called at the beginning of the package_update() function - user_package_update_success
Called when the user’s package was updated successfully - user_package_update_error
Called when an error is thrown while trying to update the user’s package - user_package_cancel_pre
Called at the beginning of the package_cancel() function - user_package_cancel_success
Called when a user’s package is cancelled successfully - user_package_cancel_error
Called when an error is thrown while attempting to cancel a user’s package - user_destruct
Called in class __destruct() function
- user_construct
Leave A Comment?
You must be logged in to post a comment.