Reputation: 761
I was wondering how to achieve an asp.net "user control" like functionality with PHP in a MVC framework, specifically CodeIgniter.
To explain better what I want, here's some guidance:
A .net user control is a file, with both aspx and code (c#/vb) that provides a functionality across every page that implement it (say, a shopping cart). It is easily added to a Master Page which is a container for every other pages in the site.
In MVC structure, a page is loaded by the controller, which loads the View. I understand I can load Views inside Views, but how to provide the controller code for "cross site views" without repeating it every main controller?
Example: I have a view that loads Categories from database into a select list and this view is on top of every page. As I shouldn't (and I'm not sure if it is even possible) access my Category model from the view, where do I put the code to load this data without having to repeat it in every function in every controller?
Upvotes: 0
Views: 1141
Reputation: 761
Answering to my own question: i found something worth checking. it provides Multiple MVC triads: https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/wiki/Home
Upvotes: 0
Reputation: 57268
I have worked very little with master pages, mainly in Sharepoint but if you want a front.master
page so to speak and then load all other pages within that context your best bet would be to create a new library as your view handler and pro grammatically design it to load the secondary view within the master.
A simple class consisting of:
class MasterViewTemplate
{
public $master = "v1.php";
private $data = array();
public function __set($key,$value)
{
$this->data[$key] = $value;
}
public function __get($key)
{
return $this->data[$key];
}
public function setMaster($master)
{
$this->master = $master;
}
public function display($page)
{
//Load the master file
//Load the inner $page file
//Inject the page contents into the master
}
}
IS this the kind of thing you was looking for.
Upvotes: 0
Reputation: 1184
As Sean said, if the functionality is at Controller level you should extend CI_Controller. May be some functionality should be at view level, so a loader view can be used to help you keep the global layout of every page and include the common features. that's something like:
[view] layout.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es" lang="es">
<head>
<?php
$this->load->view('meta', $data);
?>
</head>
<body>
<div id="wrapper">
<?php
$this->load->view('header', $data);
?>
<div id="contents">
<?php include ('menu_izq.php'); ?>
<div id="page">
<?php $this->load->view($page, $data); ?>
<div class="clear"></div>
</div>
</div>
<div style="clear: both;"></div>
<?php $this->load->view('footer');?>
</div>
</body>
</html>
In your controller you should always load this view and pass in the parameter array the value of the contents real view, like in
$data['page'] = 'incidents'; // this is the real contents
$stylesheets[] = '/scripts/jscalendar-1.0/skins/aqua/theme.css';
$data['stylesheets'] = $stylesheets;
$scripts[] = '/scripts/jscalendar-1.0/calendar.js';
$scripts[] = 'https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js';
$scripts[] = '/scripts/autoNumeric-1.4.1.js';
$scripts[] = '/scripts/autoLoader.js';
$data['scripts'] = $scripts;
$this->load->view('container',$data);
Upvotes: 0
Reputation: 160033
If you want to avoid duplicating code in each controller, then simply extend the CI_Controller
class and do all of the setup in your new Controller class upon instantiation ... and have all of your normal controllers inherit from New_Controller
.
Upvotes: 1