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?
I can't vouch that this is the best approach, but I create a base controller like this:
The view called 'base' is a template (a view that includes other views):
What this achieves is that every controller wraps its output in the base template, and that views have valid HTML instead of opening tags in one view and closing in another. If I'd like a specific controller to use a different or no template, I could just override the magic
_output()
method.An actual controller would look like this:
Then I could use its properties in my views like this (this is the 'meta' view that populates the
<head>
element):I like my approach because it respects the DRY principle and the header, footer and other elements get included just once in the code.