Search code examples
phpcodeignitercodeigniter-2

How to get the class from which a request originates in codeigniter


My modules all extend Backend controller which checks if loggedin and redirects appropriately, now I have Class FrontEnd [responsible for pages like login, signup etc] which extends My_Controller. how can I create an exemption for FrontEnd to be able to access the modules since it needs their methods to perform tasks.

this is what I have currently ....

class Backend_Controller extends MY_Controller {

// --------------------------------------------------------------------------
    public function __construct() {
        parent::__construct();
        $this->checkLoggedIn();
    }

// --------------------------------------------------------------------------
    public function checkLoggedIn() {
        //check if loggedin
        if (!$this->auth->loggedIn()) {
            //implement exclusion for FrontEnd class to allow access to methods though not        logged in
            if (!the class trying to access == 'FrontEnd') { //how to get refferal page
                return;
            } else {
                redirect('login');
            }
        }
    }
// --------------------------------------------------------------------------
}

how can I get "the class trying to access == 'FrontEnd" Edditional Info: the FrontEnd class generates a form which has an enter code hereaction= router->fetch_class() will result in someMoudleName , but what i want is for it to be able to detect that the action is coming from FrontEnd Class


Solution

  • This will depend on your url but u can do something like this..

     function getNameOfOriginatingClass{
         $this->load->library('user_agent');
         $previous_url = $this->agent->referrer();
         $url_segments = explode('/',$previous_url);
         echo '<pre>';print_r($url_segments);    
     }
    

    after printing this result u can see your link broken into parts in an array.. Normally the $url_segments[3] or $url_segments[4] will contain your previous function name and previous one will contain previous class name depending upon your url.