php - Router Error with CI -


i trying current controller's name following line:

$this->router->fetch_class(); 

i found line here:

https://gist.github.com/svizion/2325988 

this how i'm trying use in hook.

<?php if (!defined('basepath'))     exit('no direct script access allowed'); class sys_prescript {     public function is_logged_in()     {         $public_access = array('login', 'registration');             if (!in_array($this->router->fetch_class(), $public_access))          {             $user_id = $this -> session -> userdata('user_id');             if (($user_id == false) || (is_numeric($user_id) == false) && (strlen($user_id) < 5))             {                 redirect('login');             }         }     } } 

only issue i"m getting following errors.

a php error encountered severity: notice message: undefined property: sys_prescript::$router filename: controllers/sys_prescript.php line number: 9  fatal error: call member function fetch_class() on non-object in... 

i haven't tested code, try:

class sys_prescript { private $ci;  public function is_logged_in(){         $this->ci =& get_instance();         $public_access = array('login', 'registration');             if (!in_array($this->ci->router->fetch_class(), $public_access))          {             $user_id = $this->ci -> session -> userdata('user_id');             if (($user_id == false) || (is_numeric($user_id) == false) && (strlen($user_id) < 5))             {                 redirect('login');             }         }     } } 

for accessing singleton instance of ci, call hook in post_controller_constructor instead of calling in pre_controller. post_controller_constructor called after controller instantiated, prior method calls happening


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -