views:

1610

answers:

4

I am new to both the MVC pattern and PHP frameworks. I picked up a copy of CodeIgniter for Rapid PHP Application Development, but the author does not use the M part of MVC and I can't seem to get his code examples to work. CodeIgniter's User Guide is not very detailed about what goes into which component either.

What I'm looking for are examples of correct usage of MVC (preferably PHP, CodeIgniter) whose source code can be examined, maybe some guidelines, too, such as "What kind of logic can go into views" or "Should models be able to modify the data obtained from the database." I'd appreciate some tips on nesting views, too.

+1  A: 

Okay, it's not PHP, but it's a great paper that I've referred back to for many years.

http://www.javaworld.com/javaworld/jw-12-1999/jw-12-ssj-jspmvc.html

I think it's worth a read anyway, in terms of understanding MVC as applied to web apps.

Don Branson
+4  A: 

That's quite a vast topic. Put shortly, Model is where all the interaction with database happens and Controller is a middleman between Model and View.

I would recommend reading CodeIgniter forums.

Here is a couple of threads to get you started:

CodeIgniter and MVC

New to CI - some beginner questions

Best Practices in Application Organization

Just look around and you will find the answers.

SeasonedCoder
+12  A: 

Unlike what you often see, a model is not just a thin layer of access to the database. Good models are built by hand (they cannot be generated other than for the simplest of tasks) and do all the hard work. They don't know they're running in a web site.

Controllers take a web request and translate it into one or more model calls. They process the result of these calls by passing it to the view, displaying appropriate HTTP errors, doing redirects, etc. Everything that's related to the web and HTTP.

Here are example of me using this "pattern"

Controller: actionView in http://code.heukelom.net/filedetails.php?repname=rust&path=%2Ftrunk%2Flib%2Fclasses%2FRust%2FController%2FItem.php Model: http://code.heukelom.net/filedetails.php?repname=rust&path=%2Ftrunk%2Flib%2Fclasses%2FRust%2FModel%2FItem.php

See also this article:

Models themselves are often misunderstood to be a simple database abstraction layer or active record component. Mistakes like these will usually lead to very ugly-looking, sloppy, code in the controller. The controller's purpose in the big picture is to be the middle-man between the view and the model.

http://www.mikebernat.com/blog/MVC_-_Fat_Models_and_Skinny_Controllers_

Which in turn refers to this excellent one:

http://blog.astrumfutura.com/archives/373-The-M-in-MVC-Why-Models-are-Misunderstood-and-Unappreciated.html

Bart van Heukelom
Models don't really have to be built by hand? Most of the innards of the models in my Rails applications is handled by the platform, and we use a more declarative style to add functionality.
Toby Hede
What Rails generates is just data access (accessing the database). It's not really smart. The smart part of it has to be built by hand.
Bart van Heukelom
I've never quote understood MVC as "Model-View-Controller" -- I've always seen it as more "Module-View-Controller" with the module providing a the jumping off point for everything else. I changed my site to do this, and then just called my database objects models.
Nathan Loding
+1  A: 

i have the book but its basically a rewrite of the official documentation. the author doesnt add that much really. still sometimes it helps to get another view (no pun intended) on things if the official docs arent clear.

at first its quite hard to get your head round the MVC stuff but its actually very simple: in CI, all your queries, and only queries go in the model.

here is some code from a real model i use. note that this uses activerecord to simplify queries.

class mymodel extends Model { function mymodel() { parent::Model(); } function getentry($url_title) { $this->db->where('url_title', $url_title); return $this->db->get('entries')->result_array(); } function getsidebar($section) { $this->db->where('section', $section); $this->db->select('title, url_title, section'); return $this->db->get('entries')->result_array(); } }

the getentry function gets a blog post based on the url_title from the "entries" table.to use this function you do this in your controller

$blogpost = $this->mymodel->getentry($this->uri->segment(3));

you see first $this, then the name of the model and then the name of the function. $this->uri->segment(3) is the third segment of the url in domain.com/index.php/controller/function/blog-post-title

$blogpost is now an array that holds all the data you selected in your query. you then process this array:

$data = array( 'title' => $blogpost['0']['title'], 'text' => $blogpost['0']['text'], 'img' => $blogpost['0']['img'], );

now $data contains your blog post data. you then pass this to the view:

$this->load->view('myview', $data);

and now in the myview.php file you can print out the $data as such

<?php print $title ?>
<?php print $text ?>

its hard to get the formatting right, id advise to copy these examples into a text editor for easier reading. anyway this is a real world example which should help you on your way :)

stef
added: dont get too confused by all the MVC tutorials, i find they make it sound more complex than is actually the case with CI
stef