Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I disable a constructor for only a particular method in Codeigniter?

I have more than 30 methods in this User Controller.I don't want to create another controller just for this single method.

class User extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        if(empty($_SESSION['userid'])){
            $error['error'][] = "Please LogIn";
            echo json_encode($error);
            exit;
        }
    }


    public function index(){
    }

    public function get_public_pages(){

    }


}

I don't want the constructor function to run when accessing get_public_pages method.How can I do it?

like image 888
CoderMax Avatar asked Mar 03 '26 11:03

CoderMax


1 Answers

Try this,

public function __construct(){
  parent::__construct();
  $method = $this->router->fetch_method();  

  if(empty($method ) && $method != 'get_public_pages'){
    if(empty($_SESSION['userid'])){
        $error['error'][] = "Please LogIn";
        echo json_encode($error);
        exit;
    }
 }  
}
like image 195
Vinod VT Avatar answered Mar 06 '26 00:03

Vinod VT



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!