Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Implementing Hooks in Codeigniter

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 119
    Comment on it

    There are n number of situation where we want to call a function before or after an action. In Codeigniter, we can implement this by using Hooks. In-other-word, we can say Hooks are the event which can be called before or after the execution of an event.

    Steps for writing Hooks: We have to write few steps for implementing hooks in Codeigniter.

    Step 1: First we want to enable hooks after changing the file  application/config/config.php.

        $config['enable_hooks'] = FALSE;
        // Change this statement to true
        $config['enable_hooks'] = TRUE;

     

    Step 2: Now we will create a controller Codeigniter/application/controllers/User.php" with below code::

        <?php
        defined('BASEPATH') OR exit('No direct script access allowed');
        class User extends CI_Controller {
            function index(){
                echo "Welcome to our site";
            }
        }
        ?>

     

     

    Step 3: Now we will create a file "application/hooks/Hookcall.php" and save this on below path ::

        <?php
        defined('BASEPATH') OR exit('No direct script access allowed');
        class Hookcall extends CI_Controller {
            public function hookevent(){
                echo "Good Morning, Everyone";
            }
        }
        ?>

     

    Step 4: Now we will go to the "Codeigniter/application/config/hooks.php" and do the following operation on that

    $hook['pre_controller'] = array(
                                    'class'    => 'Hookcall',
                                    'function' => 'hookevent',
                                    'filename' => 'Hookcall.php',
                                    'filepath' => 'hooks'
                                    );

     

    Note: We can also do multiple calls to the same hook.Below is the code:

    $hook['pre_controller'][] = array(
                                    'class'    => 'Hookcall',
                                    'function' => 'hookevent',
                                    'filename' => 'Hookcall.php',
                                    'filepath' => 'hooks'
                                    );
    $hook['pre_controller'][] = array(
                                    'class'    => 'Hook_othercall',
                                    'function' => 'hook_otherevent',
                                    'filename' => 'Hook_othercall.php',
                                    'filepath' => 'hooks'
                                    );

     

    By this way, we will implement hooks in Codeigniter.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: