Extending controllers with hooks

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 Pagestead 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
    1. asset_construct
      Called at the end of the __construct() function; runs when the class is first instantiated
    2. asset_images_pre
      Called at the beginning of the images() function
    3. asset_images_post
      Called at the end of the images() function
    4. asset_imageUpload_pre
      Called at the beginning of the imageUpload() function
    5. asset_imageUpload_post
      Called at the end of the imageUpload() function
    6. asset_delImage_pre
      Called at the beginning of the delImage() function
    7. asset_delImage_post
      Called at the end of the delImage() function
    8. asset_destruct
      Called in class __destruct() function
  • /application/modules/auth/controllers/Auth.php
    1. auth_construct
      Called at the end of the __construct() function; runs when the class is first instantiated
    2. auth_index_pre
      Called at the beginning of the index() function
    3. auth_index_post
      Called at the end of the index() function
    4. auth_register_pre
      Called at the beginning of the register() function
    5. auth_register_post
      Called at the end of the register() function
    6. auth_payment_stripe_pre
      Called at the beginning of the payment_stripe() function
    7. auth_payment_stripe_post
      Called at the end of the payment_stripe() function
    8. auth_payment_confirm_pre
      Called the beginning of the payment_confirm() function
    9. auth_payment_confirm_post
      Called at the end of the payment_confirm() function
    10. auth_free_user_confirm_pre
      Called at the beginning of the free_user_confirm() function
    11. auth_free_user_confirm_post
      Called at the end of the free_user_confirm() function
    12. auth_payment_card_update_pre
      Called at the beginning of the payment_card_update() function
    13. auth_payment_card_update_post
      Called at the end of the payment_card_update() function
    14. auth_activate_pre
      Called at the beginning of the activate() function
    15. auth_activate_post
      Called at the end of the activate() function
    16. auth_forgot_pre
      Called at the beginning of the forgot() function
    17. auth_forgot_post
      Called at the end of the forgot() function
    18. auth_reset_password_pre
      Called at the beginning of the reset_password() function
    19. auth_reset_password_valid_user
      Called during the password reset process when a valid user has been located
    20. auth_reset_password_invalid_user
      Called during the password reset process when an invalid user has been located
    21. auth_logout_pre
      Called at the beginning of the logout() function
    22. auth_logout_post
      Called at the end of the logout() function
    23. auth_ipn_notifier_pre
      Called at the beginning of the ipn_notifier() function
    24. auth_ipn_notifier_post
      Called at the end of the ipn_notifier() function
    25. auth_stripe_hook_pre
      Called at the beginning of the stripe_hook() function
    26. auth_stripe_hook_post
      Called at the end of the stripe_hook() function
    27. auth_destruct
      Called in class __destruct() function
  • /application/modules/customdomain/controllers/Customdomain.php
    1. customdomain_construct
      Called at the end of the __construct() function; runs when the class is first instantiated
    2. customdomain_index_pre
      Called at the beginning of the index() function
    3. customdomain_index_post
      Called at the end of the index() function
    4. customdomain_index_error
      Called when no page is found
    5. customdomain_destruct
      Called in class __destruct() function
  • /application/modules/package/controllers/Package.php
    1. package_construct
      Called at the end of the __construct() function; runs when the class is first instantiated
    2. package_index_pre
      Called at the beginning of the index() function
    3. package_index_post
      Called at the end of the index() function
    4. package_create_pre
      Called at the beginning of the create() function
    5. package_create_post
      Called at the end of the create() function
    6. package_update_pre
      Called at the beginning of the update() function
    7. package_update_post
      Called at the end of the update() function
    8. package_delete_pre
      Called at the beginning of the delete() function
    9. package_delete_post
      Called at the end of the delete() function
    10. package_toggle_status_pre
      Called at the beginning of the toggle_status() function
    11. package_toggle_status_post
      Called at the end of the toggle_status() function
    12. package_destruct
      Called in class __destruct() function
  • /application/modules/sent/controllers/Sent.php
    1. sent_construct
      Called at the end of the __construct() function; runs when the class is first instantiated
    2. sent_api_pre
      Called at the beginning of the api() function
    3. sent_api_post
      Called at the end of the api() function
    4. sent_msg_pre
      Called at the beginning of the msg() function
    5. sent_msg_post
      Called at the end of the msg() function
    6. sent_destruct
      Called in class __destruct() function
  • /application/modules/settings/controllers/Settings.php
    1. settings_construct
      Called at the end of the __construct() function; runs when the class is first instantiated
    2. settings_index_pre
      Called at the beginning of the index() function
    3. settings_index_post
      Called at the end of the index() function
    4. settings_update_pre
      Called at the beginning of the update() function
    5. settings_update_post
      Called at the end of the update() function
    6. settings_update_payment_pre
      Called at the beginning of the update_payment() function
    7. settings_update_payment_post
      Called at the end of the update_payment() function
    8. settings_update_core_pre
      Called at the beginning of the update_core() function
    9. settings_update_core_post
      Called at the end of the update_core() function
    10. settings_destruct
      Called in class __destruct() function
  • /application/modules/sites/controllers/Sites.php
    1. sites_construct
      Called at the end of the __construct() function; runs when the class is first instantiated
    2. sites_index_pre
      Called at the beginning of the index() function
    3. sites_index_post
      Called at the end of the index() function
    4. sites_create_pre
      Called at the beginning of the create() function
    5. sites_create_post
      Called at the end of the create() function
    6. sites_tsave_pre
      Called at the beginning of the tsave() function
    7. sites_tsave_post
      Called at the end of the tsave() function
    8. sites_save_pre
      Called at the beginning of the save() function
    9. sites_save_post
      Called at the end of the save() function
    10. sites_siteData_pre
      Called at the beginning of the siteData() function
    11. sites_siteData_post
      Called at the end of the siteData() function
    12. sites_site_pre
      Called at the beginning of the site() function
    13. sites_site_post
      Called at the end of the site() function
    14. sites_siteAjax_pre
      Called at the beginning of the siteAjax() function
    15. sites_siteAjax_error
      Called when error is thrown in the siteAjax() function
    16. sites_siteAjax_success
      Called at the end of the siteAjax() function
    17. sites_siteAjaxUpdate_pre
      Called at the beginning of the siteAjaxUpdate() function
    18. sites_siteAjaxUpdate_success
      Called when site was updated successfully
    19. sites_siteAjaxUpdate_error
      Called when error is thrown during site updating process
    20. sites_export_pre
      Called at the beginning of the export() function
    21. sites_export_post
      Called at the end of the export() function
    22. sites_trash_pre
      Called at the beginning of the trash() function
    23. sites_trash_post
      Called at the end of the trash() function
    24. sites_updatePageData_pre
      Called at the beginning of the updatePageData() function
    25. sites_updatePageData_post
      Called at the end of the updatePageData() function
    26. sites_livepreview_pre
      Called at the beginning of the livepreview() function
    27. sites_livepreview_post
      Called at the end of the livepreview() function
    28. sites_deltempl_pre
      Called at the beginning of the deltempl() function
    29. sites_deltempl_post
      Called at the end of the livepreview() function
    30. sites_rpreview_pre
      Called at the beginning of the rpreview() function
    31. sites_rpreview_post
      Called at the end of the rpreview() function
    32. sites_getRevisions_pre
      Called at the beginning of the getRevisions() function
    33. sites_getRevisions_post
      Called at the end of the getRevisions() function
    34. sites_deleterevision_pre
      Called at the beginning of the deleterevision() function
    35. sites_deleterevision_post
      Called at the end of the getRevisions() function
    36. sites_restorerevision_pre
      Called at the beginning of the restorerevision() function
    37. sites_restorerevision_post
      Called at the end of the restorerevision() function
    38. sites_destruct
      Called in class __destruct() function
  • /application/modules/subdomain/controller/Subdomain.php
    1. subdomain_construct
      Called at the end of the __construct() function; runs when the class is first instantiated
    2. subdomain_index_pre
      Called at the beginning of the index() function
    3. subdomain_index_post
      Called at the end of the index() function
    4. subdomain_index_error
      Called when no page is found
    5. subdomain_destruct
      Called in class __destruct() function
  • /application/modules/subdomain/controllers/Subfolder.php
    1. subfolder_construct
      Called at the end of the __construct() function; runs when the class is first instantiated
    2. subfolder_index_pre
      Called at the beginning of the index() function
    3. subfolder_index_post
      Called at the end of the index() function
    4. subfolder_index_error
      Called when no page is found
    5. subfolder_destruct
      Called in class __destruct() function
  • /application/modules/subscription/controllers/Subscription.php
    1. subscription_construct
      Called at the end of the __construct() function; runs when the class is first instantiated
    2. subscription_paypal_pre
      Called at the beginning of the paypal() function
    3. subscription_paypal_post
      Called at the end of the paypal() function
    4. subscription_payment_confirmation_pre
      Called at the beginning of the payment_confirmation() function
    5. subscription_payment_confirmation_post
      Called at the end of the payment_confirmation() function
  • /application/modules/temple/controllers/Temple.php
    1. temple_construct
      Called at the end of the __construct() function; runs when the class is first instantiated
    2. temple_index_pre
      Called at the beginning of the index() function
    3. temple_destruct
      Called in class __destruct() function
  • /application/modules/user/controllers/User.php
    1. user_construct
      Called at the end of the __construct() function; runs when the class is first instantiated
    2. user_index_pre
      Called at the beginning of the index() function
    3. user_index_post
      Called at the end of the index() function
    4. user_create_pre
      Called at the beginning of the create() function
    5. user_create_post
      Called at the end of the create() function
    6. user_update_pre
      Called at the beginning of the update() function
    7. user_update_post
      Called at the end of the update() function
    8. user_send_password_reset_pre
      Called at the beginning of the send_password_reset() function
    9. user_send_password_reset_success
      Called when the function has successfully send the password reset email to the user
    10. user_send_password_reset_error
      Called when an error is thrown when trying to send the email
    11. user_details_update_pre
      Called at the beginning of the details_update() function
    12. user_details_update_success
      Called when the user’s data has successfully been updated
    13. user_details_update_error
      Called when an error was thrown while trying to update the user’s data
    14. user_login_update_pre
      Called at the beginning of the login_update() function
    15. user_login_update_success
      Called when the user’s login data was successfully updated
    16. user_login_update_error
      Called when an error is thrown while trying to update the user’s login data
    17. user_toggle_status_pre
      Called at the beginning of the toggle_status() function
    18. user_toggle_status_post
      Called at the end of the toggle_status() function
    19. user_delete_pre
      Called at the beginning of the delete() function
    20. user_delete_post
      Called at the end of the delete() function
    21. user_package_update_pre
      Called at the beginning of the package_update() function
    22. user_package_update_success
      Called when the user’s package was updated successfully
    23. user_package_update_error
      Called when an error is thrown while trying to update the user’s package
    24. user_package_cancel_pre
      Called at the beginning of the package_cancel() function
    25. user_package_cancel_success
      Called when a user’s package is cancelled successfully
    26. user_package_cancel_error
      Called when an error is thrown while attempting to cancel a user’s package
    27. user_destruct
      Called in class __destruct() function

Was this article helpful?

Related Articles

Leave A Comment?

You must be logged in to post a comment.