0

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.

4

1 回答 1

2

继承是你的朋友

您只需创建一个扩展CI_Controller的主控制器(前端控制器)

你可能会考虑做一个SPA应用程序,如果是这样的话,有很多很棒的框架可以帮助你实现这一点,相当流行的一个是angular.js

关于这个主题的一些更有用的阅读......

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
         }
    }
}
于 2013-08-14T09:06:18.307 回答