2010-08-30 14:18:47 - Article

simple mvc

Content.exe

Need a lightweight, fast, php mvc framework?

I have written a small framework named “Simple MVC” it does exactly what it says on the box, its quick to install and can be configured to handle most tasks.

Once the file is unzipped you will be presented with this structure:

Simply upload the whole structure, and the framework will work.

It is designed to be very fast and lightweight, however it can be configured, opening the file mvc_env.php within the “Simple MVC” folder displays the following preferences:

error['controller']='error';
$env->error['view']='index';
$env->error['pageNotFoundAction']='pageNotFound';
*/

// routerMap
// holds controller and action mappings to different controller, action (comma delimited)
/*
$env->routerMap['foo,bar']='index,index';
*/

// routerDeny
// holds controller and action mappings to deny (comma delimited)
/*
$env->routerDeny[]='controller,action'; // will deny the page /controller/action/
$env->routerDeny[]=''; // will deny homepage
*/

// prelayout
// defines an array of elements to be included before the main view
/*
$env->prelayout = array('header');
*/

// postlayout
// defines an array of elements to be included after the main view
/*
$env->postlayout = array('sidebar','footer');
*/

As you can see, page routing, error controllers, page denials etc can all be added here.

The whole framework is just one file and the source looks like this

url['base'] = dirname($_SERVER['SCRIPT_NAME']);
$env->url['path'] = realpath('.');
$env->url['relative'] = str_replace('//','/',dirname(__FILE__)).'/../application/';
$env->url['request'] = htmlentities($_SERVER['REQUEST_URI']);
$env->url['request_array'] = explode('/',trim($_GET['__url'],'/'));

// mvc
$env->controller = !empty($env->url['request_array'][0]) ? $env->url['request_array'][0] : 'index';
$env->action = !empty($env->url['request_array'][1]) ? $env->url['request_array'][1] : 'index';
$env->view = $env->controller;

// settings
@include_once('mvc_env.php');

// tell the controllers where to load models from
function __autoload($class_name) {
	global $env;
    require_once "{$env->url['relative']}models/$class_name.php";
}


// add custom error controller to list of denys
if (isset($env->error['controller'])){
	$env->routerDeny[] = $env->error['controller'];
}


// mapping
if (isset($env->routerMap) && in_array("{$env->controller},{$env->action}",array_keys($env->routerMap))){
	$route = explode(',',$env->routerMap["{$env->controller},{$env->action}"]);
	$env->controller = $route[0];
	$env->action = $route[1];
	$env->view = $env->controller;
}

// get the controller
if (file_exists("{$env->url['relative']}/controllers/{$env->controller}.php")) {
	require_once("{$env->url['relative']}/controllers/{$env->controller}.php");
	$controller = new $env->controller();	
	} 
	
// unset the controller if this is a denied resource, this is done on URL only, so not to interfere with any mappings
if (isset($env->routerDeny) && in_array(implode(',',$env->url['request_array']),$env->routerDeny)){
	unset($controller, $env->controller);
	}	
	
// if neither the controller or action was found set an error for the error controller to handle
if (!isset($controller) || !method_exists($controller,$env->action)){
	
	// can we run the error handler?
	if (isset($env->error)){
		$env->controller = $env->error['controller'];
		$env->action = $env->error['pageNotFoundAction'];
		$env->view = $env->error['view'];
	
		@require_once("{$env->url['relative']}/controllers/{$env->controller}.php");
		$controller = new $env->controller();	
		if (!method_exists($controller,$env->action)) {
			die("

Fatal error

Missing action: {$env->action} in controller: {$env->controller}"); } // output will be handled by the dispatcher below } else { // if we do not even have an error handler, throw a traditional 404 and die ob_start(); ?> 404 Not Found

Not Found

The requested URL url['request']; ?> was not found on this server.

{$env->action}(); // check to see if the view has been altered $env->view = isset($controller->view) ? $controller->view : $env->view; // set up the content and view $content = $controller->content; if (isset($env->prelayout)) foreach ($env->prelayout AS $element) include_once("{$env->url['relative']}/views/$element.phtml"); include_once("{$env->url['relative']}/views/{$env->view}.phtml"); if (isset($env->postlayout)) foreach ($env->postlayout AS $element) include_once("{$env->url['relative']}/views/$element.phtml");

A helloworld quickstart can be downloaded from here

The helloworld also has examples of where to instaniate the models, and how to redirect the view in the comments.

Comments.log - 0 entries

Leave a Response

System Notice: Your email address will not be published. Required fields are marked with *

LOADING...
Initializing CyberDev systems...