MVC = Model, View, Controller
Model
A model is a database table class, responsible for data source interaction and providing data to the Controller.
Table records are represented by object instances. A ‘model’ class is usually an object-relational abstraction to a table.
One implementation loads a request-specific model. Alternatively, a data abstraction layer may interact with the data source, directly.
View
a (usually HTML) presentation/view template … a page template, named for an action/function.
Controller
… manages single model (table) logic.
Front controller design pattern
A controller is used as a single entry point for request routing. The controller's central location in the request-response cycle is a suitable place to apply common logic e.g. checking credentials or setting response headers.
A controller-managed web site operates independently of the underlying web server; thus, the controller can access files from directories that would otherwise have .htaccess restrictions. A central site controller (router class) validates and loads a request, then, hands the request to the presentation/view layer.
Advantages of using an overall MVC controller are:
- Single application entry point
- Reducing scripts count, each with relative paths, database connections, authentication etc.
Implementation
Controller
Typically, a single controller (e.g. index.php) controls framework applications launch, based on request arguments.
A controller router class handles incoming URI requests, uses GET arguments to choose a module and eventually render the presentation/view layer. The model detects and assigns a presenter/view template for rendering, especially in desired formats.
Controller arguments specify the module, class, event and GET. However, the Apache mod_rewrite module ignores GET arguments and passes them to the script. For example, http://example.com/index.php?module=users&class=login
Notice that the overall controller is index.php. The controller validates the request (authentication, valid model, request sanitization etc.) and runs the requested event. For example,
- Include config.php
- Define the __autoload() function. The new PHP5 __autoload() function loads classes, assuming standardized class naming; this eliminates requiring needed libraries.
- One might employ Smarty for the presentation/view layer.
- The display() method renders the module.
- The controller could recognize different presentation/view requests and switch the module class's presentation/view layer. For example, the presentation/view layer could look at the request notice, ?view=pdf, and switch your module class's presentation/view layer to the PDF presenter, instead of the default presenter.
- The controller could create cache files or turn on caching, globally.
- Upon examining raw POST data and identifying a SOAP request, the controller could employ PHP’s new SOAP server.
- The controller could sanitize GET and POST input.
one MVC file system implementation
The /includes/ directory will utilize PHP’s new __autoload() function. Foundation classes comprise the /includes/. Each /modules/ has a configuration file, at least one module file and a template file. Modules are wrapped in an outer-page template, residing in the /tpl/ directory. Outer-page templates can use a default or a presentation/view layer for modules to render themselves.
Each "theme" or template group has its own template directory.
Config.php contains global configuration variables e.g. DSN and log file location.
Index.php is the central, application-wide controller.
Object.php is the base abstract foundation class.
Module.php is the base abstract module class. It defines the default presentation/view layer, the default module template file, the default page template file and other variables.
Direct and Indirect controller calls
Direct is the ‘ugly’ way … with the ? marks … http://host/controller.php?display=contact_formIndirect uses slashes to delimit parameters … http://host/intro/contact_form.site
Notice that the controller is not mentioned (visible). Associating the controller with the file extension .site takes place behind the scenes in an httpd.conf or .htaccess file. Apache's AddHandler configuration directive associates an executable or module with a path (e.g. /special) or a filename extension (.site). When a user requests a matching URI, Apache content handler short-circuits the normal request/response flow and passes control to the registered handler which generates the requested browser content. In .htaccess, the lines:
Action controller-test /simple.php AddHandler controller-test .tstdefine /simple.php as the “action” controller-test and associate that controller “action” with the file extension .tst. When Apache receives a request for a filename ending in .tst, Apache will execute /simple.php, instead of serving a file from disk. The executable, named by Action, is relative to the document root.
View
The model calls the Presenter::factory() to create a presenter/view child class. The factory passes a model class instance to the newly-created presenter. The presenter/view calls the model GetDate() method and a display() function.
An outer-page template includes/wraps the model template. If the model class’ $pageTemplateFile does not request an outer-page template, a default page template (/tpl/default/templates/page.tpl) is used. The page.tpl template uses the {$modulePath} and {$tplFile} directives to include the model's template. Model templates should reside in /modules/example/tpl/.
One might display/view debugging information, using a debug.php presenter/view.
Upon detecting a REST request, a rest.php presentation/view template might use the class’ $data to output XML by assigning rest (for rest.php) to this->presenter.
Articles
http://www.onlamp.com/pub/a/php/2005/09/15/mvc_intro.html
http://joestump.net/archives/759677408/php.html
Understanding MVC in PHP by Joe Stump (9/15/2005) joe@joestump.net
http://www.onlamp.com/pub/a/php/2005/11/03/mvc_controller.html
Implementing MVC in PHP: the Controller by Joe Stump (11/3/2005)
http://www.onlamp.com/pub/a/php/2006/01/26/mvc_view.html
Implementing MVC in PHP: the View by Joe Stump (1/26/2006)
http://www.oreillynet.com/pub/au/1234
Joe Stump O’Reilly articles