simple mvc

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php 
/*
 * 
 * Simple MVC settings file (this file does not need to be present)
 * Examples of different settings are contained here, just uncomment to use
 * 
 * By Adrian Callaghan 300810
 * 
 */
 
// error controller
// defines a controller to handle errors, namely the page not found
// the controller is automatically exempt from an actual page request
/*
$env->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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
 
/*
 * 
 * Simple MVC 
 * 
 * By Adrian Callaghan 300810
 * 
 */
 
 
// enviroment
$env = new stdClass();
 
// url mapping
$env->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("<h1>Fatal error</h1>Missing action: <b>{$env->action}</b> in controller: <b>{$env->controller}</b>");
			}
 
		// 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();	?>
		<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
		<html><head><title>404 Not Found</title></head><body>
		<h1>Not Found</h1>
		<p>The requested URL <?php echo $env->url['request']; ?> was not found on this server.</p>
		</body></html>
		<?php header("HTTP/1.0 404 Not Found"); die(ob_get_clean());
	}
}
 
// dispatcher /////////////////////////////////////////////////
 
//attempt to execute the action in the controller,
$controller->{$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.

VN:D [1.9.9_1125]
Rating: 10.0/10 (2 votes cast)
VN:D [1.9.9_1125]
Rating: -1 (from 1 vote)
simple mvc, 10.0 out of 10 based on 2 ratings

6,448 views

This entry was posted on Monday, August 30th, 2010 at 2:18 pm and is filed under Programming. You can follow any responses to this entry through the RSS 2.0 feed.

Leave a Reply