An MVC Framework
An MVC Framework

The View


The View of this MVC Framework is made up of two parts: The Response Class and The View Class. The Response Class handles the primitives and The View Class handles the template rendering.

The basic concept for the View is that it would be a standard/typical html file with variable substitution. The render routine readies the variables and then calls the template file in an include() This let's PHP's natural hypertext processing do the work. The result is buffered into an intrinsic variable. From there it can be processed further. e.g. 'href's can be changed to reflect the URL hierachy. The rendered View is then wrapped in a template wrapper and returned as a response.

Wrapper?

A View is just an html fragment that displays the results of an Action. It is not a proper HTML document. This way it can be returned as the result of an XmlHttpRequest() and inserted directly into the DOM. However, when returned as the response to an HTTP request, it is wrapped to make it a proper HTML document. This 'wrapper' is itself just another template. So what actually happens is that the template is embedded in the wrapper.

As it turns out, this embedding is part of the View Class and any template or Action can be embedded in any other. As can any template or Action call another template or Action. This lends itself to nested templates. So complex responses can be built from otherwise simple Requests.

HTML, JSON, XML, other?

While the default is to return an HTML document, the output format is not restricted. The View Class can just as easily return a JSON response. It's all in the template used. By default the Action Class will infer the template name unless explicitly set by the derived Action.

Example:
If the URI was "/inventory/list" then the default template would be "list.html". But if the URI was "/inventory/list.json" then the template would be set to "list.json".

Any extension can be set. The Action Class finds the extension and checks to see if the file exists. If it does then it is set as the template. If it doesn't exists then an 'error' template is set.

This 'error' template should not be confused with an HTTP 404 response. A 404 response is only returned when the URI cannot be resolved. The template used for a 404 response is set by the user or the default 'index.html' is used. This way if a human requests an invalid URI a meaningful response is returned. But if a robot requests an invalid URI then it gets a 404 response.

Meanwhile a valid request with a missing template file will return a template indicating that the properly requested template was not found. This is not a 404 response, because the URI was resolved and an Action called, the template file is missing. This is a programmer error.

The Response Class

The Response Class is fairly simple. It is designed to hold the $response, which is just a text string of the response to the Request. In addition, it has basic responses to adjust the Header for things like Bad Requests or File Not Found. The Response Class is designed to deal with the more status aspects of the Response and as such does not do any rendering. It does however, hold the intrinsic data associated with the Response. This is because it is the base class for any Response.


	class Response
	{
		private $response;
		private $infomsg;
		private $errmsg;
		private $actreqmsg;
		private $ovrlycontent;
		private $ovrlytitle;
		private $ovrlyreturn;
		private $redirect;
		
		function __construct() {
			$this->response = "";
			$this->infomsg = "";
			$this->errmsg = "";
			$this->ovrlycontent = "";
			$this->ovrlytitle = "";
			$this->ovrlyreturn = $_SERVER["HTTP_REFERER"];
			$this->redirect = false;
		}
		
		function GetResponse() {
			return $this->response;
		}
		
		function SetResponse( $response ) {
			$this->response = $response;
		}
		
		function OutputResponse() {
			if( !$this->redirect ) {
				echo $this->response;
			}
		}
		
		function Redirect( $url ) {

			if( strncmp($url, "http", 4) == 0 ) {
				header("Location: " . $url);
			} else {
				if( $GLOBALS["https"] == "on" ) {
					header("Location: https://" . $_SERVER["HTTP_HOST"] . $url);
				} else {
					header("Location: http://" . $_SERVER["HTTP_HOST"] . $url);
				}
			}
			$this->redirect = true;
		}
		
		function BadRequest() {
			header('HTTP/1.0 400 Bad Request');
		}
		
		function NotFound() {
			header('HTTP/1.0 404 Not Found');
		}
		
		function AccessDenied() {
			header('HTTP/1.0 401 Unauthorized');
		}
		
	}
	

The View Class

description coming soon ...



Back to Top
Top

© 2012 and beyond Lawrence L Hovind - All Rights Reserved