How would you structure the below page in Codeigniter?
I thought about creating seperate controllers for each section
- Left nav
- Content nav
- Login name
- Leaderboard
Excluding the content section (as this changes depending on the link on the left nav and content nav used as a kinda sub-menu). All the other sections remain roughly the same
I thought about doing:
Class User_Profile extends Controller
{
function index()
{
$this->load_controller('Left_Nav');
$this->load_controller('Content_Nav');
$this->load_controller('Login_Name');
$this->load_controller('Leaderboard', 'Board');
$this->Left_Nav->index(array('highlight_selected_page' => 'blah'));
$this->load('User');
$content_data = $this->User->get_profile_details();
$this->view->load('content', $content_data);
$this->Login_Name->index();
$this->Board->index();
}
}
Obviously this load_controller
does not exist but this functionaility would be useful. The controller for each section gets the data required from the model and then loads the page through $this->view->load()
It could be a headache to have this code in all the left nav links like News, Users, About Us, etc.. But then again not every nav link has all those sections so I need that flexability of having the sections as a "partial view"
Can anyone suggest a better way of doing this?
Best Answer
@Reinis answer probably hit the spot correctly for older versions of CI less than 2.0 however alot has changed since then, so I thought I'd answer this question with an up to date method of what I've done.
Most of it is similar to @Reinis method and also described here:http://codeigniter.com/wiki/MY_Controller_-_how_to_extend_the_CI_Controller
However here are the updates ive done:
Step 1: Create a MY_Controller.php file and store it in /application/core
Step 2: In your MY_Controller.php file put in the following contents:
Step 3: Create a sample controller to base off of MY_Controller.php, in this case I will create a welcome.php controller inside of application/controllers/ with the following content:
Once you have these controllers set, do the following:
Step 4: Create a base view inside of /application/views and name the file base.php, the content of the file should be similar to this:
Step 5: Create another view in /application/views and name this view welcome_message.php, the content of this file will be:
Once, all this is complete, you should see the following output:
As you can see
<h1>Welcome</h1>
was put into the base template.Resources:
Hope this helps anyone else coming across this technique.