How to create Hooks in Codeigniter – onlinecode

How to create Hooks in Codeigniter – onlinecode

In this post we will give you information about How to create Hooks in Codeigniter – onlinecode. Hear we will give you detail about How to create Hooks in Codeigniter – onlinecodeAnd how to use it also give you demo for it if it is necessary.

Today, we will explain to you how to create custom hooks in Codeigniter Framework. let’s know, what are hooks in Codeigniter. The hook allows you to execute a script without changing the core files.

Overview

Step 1: Enabling Hooks in CodeigniterStep 2: Define Hooks in CodeigniterStep 3: Create Controller

Step 1: Enabling Hooks in CodeigniterIn this step, we have to change the hook configuration. now we will open the “application/config/config.php” file and enable the hook.

$config['enable_hooks'] = TRUE;

Step 2: Define Hooks in CodeigniterNow, the hooks are defined in the “application/config/hooks.php” file. so you can see below code.

$hook['pre_controller'] = array(
        'class'    => 'MyClass',
        'function' => 'Myfunction',
        'filename' => 'Myclass.php',
        'filepath' => 'hooks',
        'params'   => array('beer', 'wine', 'snacks')
);

Class: The name of the class you wish to invoke. If you prefer to use a procedural function instead of a class, leave this item blank.Function: The function (or method) name you wish to call.Filename: The file name containing your class/function.Filepath: The name of the directory containing your script. Note: Your script must be located in a directory INSIDE your application/ directory, so the file path is relative to that directory.

Step 3: Create ControllerFinally, we will create a Myfirsthook.php file in the “application/hooks/” directory and paste the below code in this controller.

<?php
   class Myfirsthook extends CI_Controller { 
     
      public function index() { 
         echo "Hi, Welcome To In My First Hook";
      }
  }
?>

Now, We will define hook using the below code (on above step2).

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

Please follow and like us:

Hope this code and post will helped you for implement How to create Hooks in Codeigniter – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

69  +    =  76

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US