Implementing MVC in PHP: The Controller
Pages: 1, 2
One of the biggest advantages of MVC programming I have found is that once my foundation is rock solid, I rarely have to worry about bugs related to the basics of my site. This allows me to focus more on the application I'm programming, rather than whether my database connection is working or the user is being properly authenticated. In my own framework, I've found that there are files and classes that I haven't touched since first coding them (three years and counting). In addition to reducing the time you spend debugging on mundane features (such as authentication), working within a framework allows you to add features that propagate downstream and fix bugs in centralized locations.
Pretty URLs
Do the URLs in my example make you cringe? They make me cringe too, which is why mod_rewrite exists. Below is an example of some rewrite rules I created to work with the framework.
RewriteEngine On
# Change the URI here to whatever you want your homepage to be
RewriteRule ^/$ /index.php?module=welcome [L,QSA]
# Changes /index.php?module=welcome to /welcome
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/([^/]*)$ /index.php?module=$1 [L,QSA]
# Changes /index.php?module=users&class=login to /users/login
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/([^/]*)/([^/]*)$ /index.php?module=$1&class=$2 [L,QSA]
# Changes /index.php?module=users&class=login&event=foo
# to /users/login/foo.html
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/([^/]*)/([^/]*)/([^/]*).html$ \
/index.php?module=$1&class=$2&event=$3 [L,QSA]
Now those are some pretty URLs! One item to note is that you don't need to worry about GET arguments with mod_rewrite. It ignores GET arguments and passes them on to the real script.
Extending the Controller
The nice thing about having a controller is that you can easily make changes in a single place to add features that will then be instantly available to all of your applications. You could easily add several features to the controller to enhance your applications.
- You could automatically detect whether the request is a SOAP request by looking at raw post data. Once you had determined the incoming request was a SOAP request, you could use PHP's new
SoapServerto turn your module class into a SOAP server. - You could also use your controller to sanitize
GETandPOSTinput. This would include escaping variables, possibly removing malicious HTML code, and so on. The only drawback could be that you sanitize data that isn't truly harmful (such as data from an admin form). - The controller could instantly recognize various presentation layers and switch the module class's presentation layer. This means that the presentation layer would look at the request, notice
?view=pdf, and switch your module class's presentation layer to the PDF presenter instead of the default presenter. - You could add caching directly into your controller. That way you could use the URI to create cache files. Obviously, you'd need some additional checking to make sure caching was done intelligently, but this is another great example of how MVC programming makes life easier. If your site is bogged down and you don't have money for new hardware, you could--globally in the controller--turn on caching with little effort.
Whatever you decide to do with your controller, make sure that the changes are ambiguous enough to be useful for all applications, regardless of what they might be used for. For instance, stripping all HTML from POST input might not be such a good idea, as many administrative forms might need to add/edit/update data in the database that contains HTML.
Up Next
In the next part of this series, I will cover the presentation layer (the view). By using the factory method, I can easily switch the presentation of my applications to the users.
Joe Stump is the Lead Architect for Digg where he spends his time partitioning data, creating internal services, and ensuring the code frameworks are in working order.
|
Related Reading PHP in a Nutshell |
Return to the PHP DevCenter.
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 5 of 5.
-
controller with html-frameset
2006-02-17 07:08:51 pete06 [Reply | View]
Hi there!
I am playing a little around with the framework and stumbled on making a HTML-frameset for the view.
Has anybody an idea how to implement this clean?
Tanks in advance,
yours
Pete
-
working sample
2005-11-04 18:43:38 maeve_zahra@yahoo.com [Reply | View]
this could be something nice to read but since no working example, then how can we test it?
i think other then waiting for 2 month for 1 series, why not let people try the full and working sample from this article and got the explain later.
anyway, thanks for writing this.
regards,
ryan
ps. regarding other mvc framework, i though you want to test this: http://sourceforge.net/projects/webframework
-
Re: What would you like Joe to cover next? Let him know here.
2005-11-04 06:48:09 trollll [Reply | View]
I'd like to see Joe follow up this article with how to make the code in this one secure and explaining why this code opens the system up like nobody's business.
And maybe after that covering the reasons why this would make more sense to implement as a controller object rather than many nested "if" statements. Though I realize that creating an object here would add to the lines of code and potentially scare some people out of trying out an MVC...




the above-mentioned mod_rewrite rueles didn't work for me. so i did some modifications of the regexs. the following works fine for me:
############################################
RewriteEngine on
Options +FollowSymLinks
RewriteRule index.php - [L]
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^(.[^/]*)[/]?$ /index.php?module=$1 [L,QSA]
#RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^(.[^/]*)/(.[^/]*)[/]?$ /index.php?module=$1&class=$2 [L,QSA]
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^(.[^/]*)/(.[^/]*)/(.[^/]*)[/]?$ /index.php?module=$1&class=$2&mode=$3 [L,QSA]
############################################