How Is MVC Supposed to Work in Codeigniter

How is MVC supposed to work in CodeIgniter

Despite its claims to the contrary, Codeigniter does not use MVC at all (In fact, very few web frameworks do!) it actually uses a PAC (Presentation-abstraction-control) architecture. Essentially, in PAC the presentation layer is fed data by a presenter (which CodeIgniter calls a controller) and in MVC the View gets its own data from the model. The confusion exists because of this mislabeling of MVC.

As such, CodeIgniter (and most of the other popular web frameworks) don't encourage a proper model but just use very rudimentary data access its place. This causes a large set of problems because of "fat controllers". None of the code which relates to the domain becomes reusable.

Further reading:

  • MVC vs. PAC

  • The M in MVC: Why Models are Misunderstood and Unappreciated

  • Model-View-Confusion part 1: The View gets its own data from the Model

  • The Fat Controller

This is a widespread point of confusion in the PHP Community (The MVC vs. PAC article identifies the problem as stemming from the PHP community. that was written in 2006 and nothing has changed, if anything it's got worse because there are more frameworks and tutorials teaching an erroneous definition of MVC.) and why people looking at "MVC" PHP code who have come from a background outside web development understandably become confused. Most "MVC" implementations in PHP are not MVC. You are correct in thinking models should be properly structured, it's CodeIgniter that's wrong to label itself as MVC.

Codeigniter and MVC

can data from the database be pulled from the model and stored? for example could the model hold an array of names say that I have queried back from the database?

The purpose of the model is to represent the data/state of the application. The implementation (data pulled and cached in a model object) doesn't matter. The model is supposed to allow the view to show itself and allow the controller to modify itself. There should be no business logic in the model (that's what the controller is there for). So, yes, the model can store data from a database.

could I then display these to the user 1 by 1 using a button to increment a pointer to point to the next name in the array for example

The presentation logic, or how you display a model to the user is a responsibility of the view. The model should allow the view to query the data, and then display it.

CodeIgniter MVC: multiple models per controller?

It's really as simple as you're thinking. Models in Codeigniter are just supposed to passed db results to the controller, or custom class, then to the controller.

You can load as many models as you like in a controller - thats not a problem at all. Personally I try to keep my model > db table in a one to one relationship. If I have work to do on the data from the model, i'll usually add a Library (custom class) to handle that part.

That way my controller stays clean.

MVC Codeigniter - How is the controller called from the view?

as mentioned earlier you don't call a controller from the view normally. That goes against MVC. What you can do, if I am getting your question right, you can add a URL that points to your controller as a value to your select option tag. Than you can handle the selection from javascript as an onselect event. You just give the selected value to window.location.href and you will be redirected to your controller.

CodeIgniter MVC - Is it a good practice to have Models serve views?

Better you create a custom library and put getNoticeboardHTML() in that class. After that call this wherever you need. Do not dirty your model with HTML.

Create Custom Library

Right MVC concept at pass DB data to view

In MVC, the view would request the data from the model directly and would not be passed data by anything. The view has a dependency on the model and requests its own data. See this answer: How is MVC supposed to work in CodeIgniter for a more detailed description on why this is so.

The answer is to initialise the view after the model and pass the view the entire model in the constructor.

e.g.

class View {
private $model;

public function __construct(my_model $model) {
$this->model = $model;
}

public function output() {
$html = '<ul>';
foreach ($this->model->getUserData() as $row) {
$html .= '<li>' . $row->name . '</li>';
}
$html .= '</ul>';
return $html;
}
}


Related Topics



Leave a reply



Submit