CodeIgniter Forums

Full Version: Question about MVC design patterns..
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Essobi[/eluser]
I have a question regarding standard practices in MVC... Is it generally better to represent all your data from multiple controller with a single view if it's possible to do so? It would make sense to me, to consolidate all your views into one, if possible then customize the view with a switch statement on data provided from the controller..


Essobi

El Forum

[eluser]Michael Wales[/eluser]
I would only consolidate views that made sense: for instance - adding and editing data, virtually the same thing (for each different model).

But no, for the most part, your views are so different it would cause a total version control disaster trying to manage them all from within one file (not to mention, be F-ing hideous).

El Forum

[eluser]Essobi[/eluser]
Okay.. Say I have 10 tables in a DB.. Each 10 tables has it's own controller, applying it's own business logic to the data returned from one of the 10 models, for each table. Each table is then rendered in a view, to the enduser for the resulting dataset.. So.... I should have 10 seperate views instead of one even if I create the HTML table in the controller application and let the views handle the nav/header/footer decisions? Or is creating the HTML table in the controller and handing it to the view another no-no?

Part of the reason I ask this.... I'm implementing some order by columns in the table headers for some CRUD controllers ATM.. I essentially want the same nav on the entirety of the application, and all the views essientially have the same view.. one table.

El Forum

[eluser]Luipaard[/eluser]
There is a better way for you to achieve what you want: helpers.
Then you'll let the helper create the table header and handle the sorting, while keeping the seperated views.

El Forum

[eluser]mvcwannabe[/eluser]
i imagine data should be built up in the model where it would be potentially retrieved from a database and altered / composed into an object structure suitable for use by the view.
the view which contained the skeleton for said table would then invoke methods on the model objects the controller passed it references to.

but then there is the mvc thing where the view supposedly is an observer of the model, therefore the controller wouldnt have to pass model object references to the view. the model would in theory invoke some sort of update method on the view and the view would have access to the references that way.

im a bit unclear on the details..

El Forum

[eluser]Essobi[/eluser]
I was looking at using the HTML table class and the URL helper inside my controller... Something like.


$orderById = anchor(base_url().'ManageAuctions', 'ID#', array('title' => 'Order By..'));
$orderByAuctionDate = anchor(base_url().'ManageAuctions', 'Auction Date', array('title' => 'Order By..'));
$orderByActive = anchor(base_url().'ManageAuctions', 'Active', array('title' => 'Order By..'));
$this->load->library('table');
$this->table->set_heading(array($orderById, $orderByAuctionDate, $orderByActive));
$tmpl = array (
'table_open' => '<table border="0" cellpadding="2" cellspacing="2">',

'heading_row_start' => '<tr align="left" bgstyle="color: #ffffff">',
'heading_row_end' => '</tr>',
'heading_cell_start' => '<th nowrap>',
'heading_cell_end' => '</th>',

'row_start' => '<tr bgstyle="color: #aaaaaa">',
'row_end' => '</tr>',
'cell_start' => '<td nowrap>',
'cell_end' => '</td>',

'row_alt_start' => '<tr bgstyle="color: #eeeeee">',
'row_alt_end' => '</tr>',
'cell_alt_start' => '<td nowrap>',
'cell_alt_end' => '</td>',

'table_close' => '</table>'
);
$this->table->set_template($tmpl);
$data['getAllEntries'] = $this->table->generate($this->manageauctions_model->get_all_listings());
$this->load->view('manageAuctions_view', $data);

(as noted from post... http://ellislab.com/forums/viewthread/54084/)

El Forum

[eluser]Michael Wales[/eluser]
If I was just making a basic CRUD, where the user directly influenced the data within the database, yes - I would make one view.

I would define all of my models, create one controller to return the metadata of the table, and then generate a form that reflects that table (using the metadata) within my view.

To be honest, I find these not very user-intuitive at all and only useful for the bare-bones, just get some data in there to work with, type applications. For those, I'll just use phpMyAdmin.

El Forum

[eluser]Essobi[/eluser]
eventually it'll evolve into a full app, where each controller/model is doing specific things.. joins, data format changes, etc, but I can still see the view being just one... view. Whence the view would need modified to something outside of that standard template.. the controller would be modified to call the appropriate one.. I'm really new to MVC/design patterns.. Maybe this is the old C programmer coming out..

El Forum

[eluser]mvcwannabe[/eluser]
we oop wannabes do favor delegation over conditionals Smile

like rather than

switch(state) {
case:
do thing 1
case:
do thing 2
....
}

oop wannabes like

state->case()