[eluser]Edemilson Lima[/eluser]
Quite long thread... I find an example there which simplify the thing, but still is not the perfect world. The products.php controller could be:
Code:
<?php
class Products extends Controller {
function Products() {
parent::Controller();
$this->load->helper('form');
$this->load->model('categories');
$this->load->model('products');
$header['menu']=$this->load->view('menu_view','',true);
$categories=$this->model->categories->list();
$lateral['lateral']=$this->load->view('categories_view',$categories,true);
$this->data['header']=$this->load->view('header_view',$header,true);
$this->data['lateral']=$this->load->view('lateral_view',$lateral,true);
$this->data['footer']=$this->load->view('footer_view','',true);
}
function index() {
// generates a list of products with links to add, update or remove
$products=$this->model->product->list();
$this->data['section']='Product List';
$this->data['contents']=$this->load->view('products_view',$products,true);
$this->load->view('main_view',$this->data);
}
function add() {
// return a form to include new products, etc.
$product_data=product_add_function();
$this->data['section']='Product - Include';
$this->data['contents']=$this->load->view('product_include_view',$product_data,true);
$this->load->view('main_view',$this->data);
}
function update($id) {
// generates a form filled with a product data
$product_data=product_update_function($id);
$this->data['section']='Product - Update';
$this->data['contents']=$this->load->view('product_update_view',$product_data,true);
$this->load->view('main_view',$this->data);
}
function remove($id) {
// ask to remove and deletes products from the database
$product_data=product_remove_function();
$this->data['section']='Product - Remove';
$this->data['contents']=$this->load->view('product_remove_view',$product_data,true);
$this->load->view('main_view',$this->data);
}
}
?>
In this example, the common views be in the constructor and are passed to the main_view by $this->data variable.
Well, I could place these common views in a function and just load it everytime I need.
I take a brief look at the examples there, but what I really want (I guess) was a way to load controllers within views or load constructors instead. We could have something like:
Code:
$this->load->constructor('[folder/]file','function'[,$data][,true/false]);
Such method could load the file and execute the function, passing the optional $data array and print or return the output to the caller constructor or view. Loaded constructors could also load another views or constructors.
Well, I don't know the impact of this or even if it is possible, but I think it could solve this issue.