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 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)