• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Call one controller from another controller

#1
[eluser]Unknown[/eluser]
Sorry if this is a n00b question but I'm having a hard time wrapping my head around this...

I have a site controller, this is responsible for gathering required data and loading views (Queries are hard coded while I'm testing):

Code:
class Site extends Controller {

function userView() {
        $this->load->model('image_model');

        if ($query = $this->image_model->getAlbumsByUser(2)) {
            $data['albums'] = $query;
        }

        $this->load->view('header');
        $this->load->view('user_view', $data);
        $this->load->view('footer');
    }

I also have a user controller which manages logging in etc.

Code:
class User extends Controller {

    function login() {

        $username = $this->input->post('username');
        $password = $this->input->post('password');

        $this->load->model('user_model');

        if ($this->user_model->authenticateUser($username, $this->_prepPassword($password))) {

            $sessiondata = array(
                'username' => $username,
                'authenticated' => true
            );

            $this->session->set_userdata($sessiondata);
            $this->load->view('user_view'); ***********************
        }
    }

Where the stars are I want to load the user_view, but as this view relies on certain data being passed to it, I want to load it by first calling Site->userView(). How do I achieve this? Or am I not understanding MVC properly?

#2
[eluser]WanWizard[/eluser]
After validating the login, store the logged-in state in the session, and use redirect to load the site controller.

#3
[eluser]Prophet[/eluser]
Can't User just load image_model? I wouldn't be too concerned with code repetition, especially simple stuff like loading views.

Code:
class User extends Controller {

    function login() {

        $username = $this->input->post('username');
        $password = $this->input->post('password');

        $this->load->model('user_model');
        $this->load->model('image_model'); //Autoload it if you call it often

        if ($this->user_model->authenticateUser($username, $this->_prepPassword($password))) {

            if ($query = $this->image_model->getAlbumsByUser(2)) {
                $data['albums'] = $query;
            }

            $sessiondata = array(
                'username' => $username,
                'authenticated' => true
            );

            $this->session->set_userdata($sessiondata);
            $this->load->view('user_view', $data);
        }
    }

Edit: If you really want to call Site->userView() from User, just have User extend Site (since Site sounds like it would have some generic methods that will be useful to many controllers throughout the site?)


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.