Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not load url helper in codeigniter

I tried to load base_url() in controller, but codeigniter does not load the helper('url'). I also call helper from autoload and the constructor both in the hook, but it's still not working and showing an error "Trying to get property of non-object".

Any idea how can I redirect?

My code:

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

class Auth_hook {

    protected $CI;

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

    public function index(){
      redirect(base_url('auth/login'));

      print_r("hello!!");
      if(isset($_SESSION['name']) == 'TRUE'){
        redirect(base_url('auth/admin'));
      }
      else {
        redirect(base_url('auth/login'));
      }
    }
}
like image 945
Masoud Tavakkoli Avatar asked Jan 29 '26 15:01

Masoud Tavakkoli


1 Answers

How about this:

class Auth_hook {

  protected $CI;

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

  public function index(){
    // can communicate back with CI by using $this->CI
    $this->CI->load->helper('url');

    redirect(base_url('auth/login'));

    print_r("hello!!");
    if(isset($_SESSION['name']) == 'TRUE'){
      redirect(base_url('auth/admin'));
    }
    else {
      redirect(base_url('auth/login'));
    }
  }
}
like image 154
Ilanus Avatar answered Jan 31 '26 06:01

Ilanus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!