CodeIgniter Hooks

CodeIgniter Hooks acts like middleware, Sometimes there is a need to restrict some pages to access without login or without admin permission then hooks are a very useful feature of Codeigniter. In CodeIgniter pages run by controller function so if we want to validate permission then we have to write code in each function of the controller, in this case, hooks save our time and code also.

CodeIgniter Hooks like laravel middleware which validates code before executing controller functions. Let’s see the following steps in which we have used a hook to restrict the page:


Step1:
Download CodeIgniter3

 https://codeigniter.com/download 


Step2: 
Create Blocker.php in application>hooks folder, write the following code:

<?php 
 class Blocker { 
     function requestBlocker(){ 
        
                $ci=&get_instance(); 
                if($ci->is_hookable){ 
     
             echo "not allowed"; 
             die; 
        
                } 
                
     } 
 } 
 ?> 
   


Step3:
 Go to the application>config>hooks.php file, write the following code:

<?php 
   
 $hook['post_controller'] = array( 
         'class'    => 'Blocker', 
         'function' => 'requestBlocker', 
         'filename' => 'Blocker.php', 
         'filepath' => 'hooks', 
         'params'   => "" 
 ); 
   


Step4:
Create a Controller Test.php,write the following code:

<?php 
 defined('BASEPATH') OR exit('No direct script access allowed'); 
   
 class Test extends CI_Controller { 
   
             /** 
              * Index Page for this controller. 
              * 
              * Maps to the following URL 
              *                      http://example.com/index.php/welcome 
              *          - or - 
              *                      http://example.com/index.php/welcome/index 
              *          - or - 
              * Since this controller is set as the default controller in 
              * config/routes.php, it's displayed at http://example.com/ 
              * 
              * So any other public methods not prefixed with an underscore will 
              * map to /index.php/welcome/<method_name> 
              * @see https://codeigniter.com/user_guide/general/urls.html 
              */ 
               
               public $is_hookable = FALSE; 
               
             public function index() 
             { 
                         $this->load->view('test'); 
             } 
 } 


In this Controller we have written: Public $is_hookable=FALSE, means this controller is not hook able.


Step5:
In Welcome.php Controller, default Controller of CodeIgniter, write the following code:

<?php 
 defined('BASEPATH') OR exit('No direct script access allowed'); 
   
 class Welcome extends CI_Controller { 
   
             /** 
              * Index Page for this controller. 
              * 
              * Maps to the following URL 
              *                      http://example.com/index.php/welcome 
              *          - or - 
              *                      http://example.com/index.php/welcome/index 
              *          - or - 
              * Since this controller is set as the default controller in 
              * config/routes.php, it's displayed at http://example.com/ 
              * 
              * So any other public methods not prefixed with an underscore will 
              * map to /index.php/welcome/<method_name> 
              * @see https://codeigniter.com/user_guide/general/urls.html 
              */ 
               
               
             
        public $is_hookable = TRUE; 
    
               
             public function index() 
             { 
                         $this->load->view('welcome_message'); 
             } 
 } 
   


In this we have written public $is_hookable= TRUE means, this controller is restricted, it will show an error: not allowed.

 


Step6:
Create a test.php file in views, write the following code:

<?php 
 defined('BASEPATH') OR exit('No direct script access allowed'); 
 ?><!DOCTYPE html> 
 <html lang="en"> 
 <head> 
             <meta charset="utf-8"> 
             <title>Welcome to CodeIgniter</title> 
   
             <style type="text/css"> 
   
             ::selection { background-color: #E13300; color: white; } 
             ::-moz-selection { background-color: #E13300; color: white; } 
   
             body { 
                         background-color: #fff; 
                         margin: 40px; 
                         font: 13px/20px normal Helvetica, Arial, sans-serif; 
                         color: #4F5155; 
             } 
   
             a { 
                         color: #003399; 
                         background-color: transparent; 
                         font-weight: normal; 
             } 
   
             h1 { 
                         color: #444; 
                         background-color: transparent; 
                         border-bottom: 1px solid #D0D0D0; 
                         font-size: 19px; 
                         font-weight: normal; 
                         margin: 0 0 14px 0; 
                         padding: 14px 15px 10px 15px; 
             } 
   
             code { 
                         font-family: Consolas, Monaco, Courier New, Courier, monospace; 
                         font-size: 12px; 
                         background-color: #f9f9f9; 
                         border: 1px solid #D0D0D0; 
                         color: #002166; 
                         display: block; 
                         margin: 14px 0 14px 0; 
                         padding: 12px 10px 12px 10px; 
             } 
   
             #body { 
                         margin: 0 15px 0 15px; 
             } 
   
             p.footer { 
                         text-align: right; 
                         font-size: 11px; 
                         border-top: 1px solid #D0D0D0; 
                         line-height: 32px; 
                         padding: 0 10px 0 10px; 
                         margin: 20px 0 0 0; 
             } 
   
             #container { 
                         margin: 10px; 
                         border: 1px solid #D0D0D0; 
                         box-shadow: 0 0 8px #D0D0D0; 
             } 
             </style> 
 </head> 
 <body> 
   
 <div id="container"> 
             <h1>Welcome to CodeIgniter!</h1> 
   
             <div id="body"> 
                         <p>The page you are looking at is being generated dynamically by CodeIgniter.</p> 
   
                         <p>If you would like to edit this page you'll find it located at:</p> 
                         <code>application/views/welcome_message.php</code> 
   
                         <p>The corresponding controller for this page is found at:</p> 
                         <code>application/controllers/Welcome.php</code> 
   
                         <p>If you are exploring CodeIgniter for the very first time, you should start by reading the <a href="user_guide/">User Guide</a>.</p> 
             </div> 
   
             <p class="footer">Page rendered in <strong>0.0244</strong> seconds. <?php echo  (ENVIRONMENT === 'development') ?  'CodeIgniter Version <strong>' . CI_VERSION . '</strong>' : '' ?></p> 
 </div> 
   
 </body> 
 </html> 
   


Step7:
Do not delete the default view file welcome_message.php

Categories: web development codeigniter

Trending Courses

CodeIgniter

Regular : 45 Days

Fastrack : 20 Days

Crash : 10 Days

Advance Digital Marketing

Regular : 6 Months

Fastrack : 3 Months

Crash : 2 Months

React JS

Regular : 45 Days

Fastrack : 20 Days

Crash : 7 Days

Laravel

Regular : 45 Days

Fastrack : 20 Days

Crash : 10 Days

Front End Developer

Regular : 7 Month

Fastrack : 4 Month

Crash : 2 Month

Related Blogs

Request For Demo