CodeIgniter Forums
Two views one controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: Two views one controller (/showthread.php?tid=62772)



Two views one controller - snape - 08-24-2015

Hey!

I just have one controller:
  • Students

And two views:
  • students_v //Show a list of all students
    form_students_v //the page that has a form to create a new student

All that i want is to make the Students controller handles all the methods. I dont want to create another controller called Form_studens.

So... How can i make the original Students take care of the two views? Is the best practice create a method inside the Students to load the form_students_v page? I should use a helper to load that?

Its so confusing to me...

Tks


RE: Two views one controller - pdthinh - 08-24-2015

(08-24-2015, 05:16 PM)snape Wrote: Hey!

I just have one controller:




  • Students

And two views:




  • students_v    //Show a list of all students
    form_students_v //the page that has a form to create a new student

All that i want is to make the Students controller handles all the methods. I dont want to create another controller called Form_studens.

So... How can i make the original Students take care of the two views? Is the best practice create a method inside the Students to load the form_students_v page? I should use a helper to load that?

Its so confusing to me...

Tks
One controller can have as many methods as you want. Maybe I show a skeleton:


PHP Code:
class Students extends CI_Controller

{
public function 
__construct()
{
// init student
}

// method for list all students (default)
public function index()
{
// get all students and store in $data
// load view file
// e.g: $this->load->view('students_v', $data);
}

// method for edit a student
public function edit($id)
{
// get the student by $id and store in $data
// load view file
// e.g: $this->load->view('form_students_v', $data);
}

// Students 


You can read more http://www.codeigniter.com/user_guide/general/controllers.html#methods


RE: Two views one controller - snape - 08-25-2015

Thks, man.