douhu2131 2013-08-13 22:31
浏览 10
已采纳

获取用户数据

There are two parts to this application I've been building. There's the website which is powered by the CMS and there's the CMS (wrestling manager) that goes with it. With the two controllers I created called frontend and backend I use those accordingly. The frontend controller is responsible for the code that needs to be ran across all controllers on the website. The backend controller is responsible for the code that needs to be ran across all controllers on the CMS.

I'm looking for the ideal way to work with the user data after the user successfully logs in again gets directed to the dashboard which is extended by the backend controller. I've heard different solutions for example a hook. That's just one I've heard.

  • 写回答

1条回答 默认 最新

  • dsdzvprlp51692469 2013-08-14 09:06
    关注

    Inheritance is your friend here

    You would simply create a master controller(front-controller) that extends CI_Controller

    You might consider doing a SPA application, if so there are many great frameworks to help you achieve this, quite a popular one is angular.js

    some more useful reading on the subject...

    class MY_Controller extends CI_Controller
    {
    
        protected $currentUser;
    
        protected $template;
    
        public function __construct()
        {
            //You talked about hooks
            //this constructor is the same as saying
            //post_controller_constructor
    
            $this->template = 'master/default';
        }
    
        //Ok so what functions need to be run
        //throughout the application
        //run them once here, keeping our app DRY
    
        protected function isAjax()
        {
            return ($this->input->is_ajax_request()) 
                   ? true 
                   : show_error('Invalid Request!');
        }
    
        protected function isLoggedIN()
        {
           //load your auth library maybe here
           $this->load->library('auth');
    
           //check you have a "current logged In user"
           //expect object or null/false
           $this->currentUser = $this->auth->getLoggedInUser();
    
           return ($this->currentUser) ?: false;
        }
    }
    
    class Frontend_Controller extends MY_Controller
    {
        public function __construct()
        {
           parent::__construct();
        }
    
        public function testFirst()
        {
             //maybe you just need to test for ajax request here
             //inheritance from parent
             $this->isAjax();
        }
    
        public function testSecond()
        {
            //maybe you DO need a user logged in here
             //inheritance from parent
             $this->isAjax();
    
             //or yet again, abstract this to the parent level
             if(!$this->currentUser || is_null($this->currentUser))
             {
                 //handle errors on frontend
                 return $this->output->set_status_header(401); //un-authorized
             }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序