如何在codeigniter中获取请求所源自的类

时间:2014-02-26 08:17:31

标签: php codeigniter codeigniter-2

我的模块都扩展了Backend控制器,它检查登录和重定向是否正确,现在我有Class FrontEnd [负责登录,注册等页面],扩展了My_Controller。 如何为FrontEnd创建一个豁免,以便能够访问模块,因为它需要他们的方法来执行任务。

这就是我目前所拥有的......

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');
            }
        }
    }
// --------------------------------------------------------------------------
}

如何让“试图访问的类=='FrontEnd” 传统信息: FrontEnd类生成一个具有的表单 enter code here action = router-> fetch_class()将导致someMoudleName, 但我想要的是它能够检测到该动作来自FrontEnd Class

1 个答案:

答案 0 :(得分:0)

这取决于你的网址,但你可以做这样的事情......

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

打印完这个结果后,你可以看到你的链接被分成数组中的部分。通常$ url_segments [3]或$ url_segments [4]将包含你以前的函数名,而前一个函数名将包含以前的类名,具体取决于你的网址。