user2576961
user2576961

Reputation: 415

Getting user data

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.

Upvotes: 0

Views: 72

Answers (1)

Philip
Philip

Reputation: 4592

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
         }
    }
}

Upvotes: 2

Related Questions