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

五迷三道 提交于 2020-01-25 00:18:45

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/22035483/how-to-get-the-class-from-which-a-request-originates-in-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!