#native_company# #native_desc#
#native_cta#

Build an MVC Framework with PHP Page 2

By Keith Vance
on December 17, 2009

The View
The View component of your application consists of two parts: simple PHP scripts and HTML templates.
Amalgamate’s HTML templates are Smarty templates. Smarty is the most mature, robust, and developer-friendly templating system available for PHP. With Smarty, you keep all of your PHP out of your HTML.
The simple PHP scripts for Amalgamate will reside in your web server document directory. These scripts are what the user hits with their browser. The basic functionality here is to instantiate any objects you may need (such as a Smarty template object), call the necessary methods to do what the user wants, and then render the HTML (i.e., View).
You call object methods here in the View, but you don’t define business logic–that’s what the Controller is for. The only PHP code you put in these PHP scripts relates to making the Viewer work properly. Actually, you also include the necessary files (i.e., PHP classes) and set up the database connection. You then pass a reference to that database connection to your classes when you instantiate objects. See Listing 3 for the complete Viewer code (index.php).
Author Note: In Amalgamate, the PHP scripts and the Smarty templates all exist within the web server document directory for ease of use. However, I highly recommend you store your HTML templates and Smarty’s cache directory outside the web server document root.
Author Note: If you get this message: Fatal error: Smarty error: unable to write to $compile_dir, you need to adjust the permissions on the ctemplates directory under AmalgamateTemplates so that the web server can write to it.
The Controller
The Controller component of Amalgamate is where your business logic goes. This is where you put the nuts and bolts of your application. And you do that by setting up several classes that know which stored procedures to call in order to fetch or manipulate the database.
Your simple Viewer PHP scripts instantiate these Controller objects and call the necessary methods. Your Smarty templates then render the appropriate member variables in the user’s web browser. The Controller objects will throw exceptions when they run into errors, and the Viewer PHP will react appropriately by either dying or rendering an error page. See Listing 4 for the complete listing of all Amalgamate classes (Amalgamate.php).
Author Note: Like your Smarty templates, Amalgamate’s Controller classes are stored in the web server document root. However, for security purposes, I highly recommend you store these classes outside of the web server document directory. As a general rule, make only the files that need to be accessible to the web accessible–usually Viewer PHP scripts and accompanying images.
MVC as Best Practice
The double-edged sword of PHP is that unlike Java, which forces you into an object-oriented mindset and pushes you towards writing software in a particular style, it allows you to design your application however you want to. Take it from someone who’s been writing PHP code for more than 12 years, the Model-View-Controller architecture is the best way to go. In fact, any seasoned developer knows that proper decoupling, encapsulation, and abstraction are required when building quality software.
Since the release of PHP 5, the once clunky server-side procedural scripting language has become a powerful object-oriented programming environment. And if you combine the power of PHP with a nice templating system such as Smarty and a powerful database such as PostgreSQL, you can cleanly isolate the Viewer of your application from the data Model and Controller. Now you know how.
About the Author
Keith Vance is a software engineer and a journalist. He’s been developing web applications professionally since 1997, and he received his journalism degree from the University of Washington in 2008. to e-mail him.