php - hooks didn't load in codeigniter

630

I'm new in codeigniter, I want 2 page with the name of "login" and "admin" i use hook and session to detect user is logged in or not, but unfortunately hook didn't load (Call) at all. any idea?
My controller :

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Auth extends CI_Controller{

    public function __construct(){
      parent::__construct();
    }


    public function index(){

    }
}


My hook :

<?php  if ( !defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );

class Auth_hook {

    protected $CI;

    public function __construct() {
      $this->CI =& get_instance();
    }

    public function index(){
      print_r("hello!!");
      }
    }
}
?>

EDIT: And finallyhooks.php :

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

Answer

Solution:

Try this :

$hook['pre_controller']

Instead of this :

$hooks['pre_controller']
135

Answer

Solution:

Did you check code manual hooks?

application/config/hooks.php:

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

application/hooks/MY_CLASS.php:

<?php    
function Myfunction(){
    echo "HOOK PROBLEM .. !!";
    die;
}
?>

For that, I move into this page and get a 'HOOK PROBLEM .. !!'.

EDIT:

Your code is doing what you want. You need to check that the login before you do any kind of redirect.

If (user is not logged in) {
    If (Current route is not a login page) {
        Redirect user to login page
    }
}

People are also looking for solutions to the problem: php - Mailer Error: SMTP connect() failed

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.