Adrian Callaghan`s Blog

Programming

simple mvc

by Adrian on Aug.30, 2010, under Programming, Work

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:F [1.9.9_1125]
Rating: 10.0/10 (1 vote cast)
VN:F [1.9.9_1125]
Rating: 0 (from 0 votes)
Leave a Comment more...

sort a array by its value

by Adrian on Jul.29, 2010, under Programming, Work

To sort an array by its value, similiar to mysql sort by just use

1
usort($array, create_function('$a, $b, $sortBy="distance"', 'return strnatcmp($a[$sortBy], $b[$sortBy]);'));

$array stores the array
$sortBy set the key to sort by

i.e

1
2
3
4
5
6
7
$array = array (
    1=>array('name'=>'me','location'=>'somewhere','distance'=>'200'),
    2=>array('name'=>'you','location'=>'somewhere','distance'=>'2') 
    );
 
usort($array, create_function('$a, $b, $sortBy="distance"', 'return strnatcmp($a[$sortBy], $b[$sortBy]);'));
print_r($array);

will result in the array being re-ordered with location 2 now being in location 1 (preserving the keys)

VN:F [1.9.9_1125]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.9_1125]
Rating: +2 (from 2 votes)
Leave a Comment more...

styling form buttons with divs

by Adrian on Aug.28, 2009, under Programming

Styling a button inside a form with multiple div`s to achieve a curved button with click events etc, can be done relatively easy with the use of javascript.

For example:



firstly lets take a look at our raw code

<form method="post" action="">
      <input type="submit" name="submit">
</form>

Next we place the button within no script tags, so it only displays if javascript is switched off, so that
non javascript enabled browsers can still submit.

<form method="post" action="">
      <noscript>
          <input type="submit" name="submit">
      </noscript>
</form>

Now we add the javascript styled button

 
<form method="post" action="" name="mainForm">
      <noscript>
          <input type="submit" name="submit">
      </noscript>
	<script type="text/javascript">
	<!--					
 
	// do the submission
	function submitform(){
		document.mainForm.submit();
		}
 
	// print the button
	document.write('<a href="javascript:submitform();" style="text-decoration:none; color:#fff; font-weight:bold;">');
 
	document.write('<div id="buyButtonMid">');
	document.write('<div id="buyButtonLeft"></div>');
	document.write('submit');
	document.write('<div id="buyButtonRight"></div>');
	document.write('</div>');
 
	document.write('</a>');
 
 
	// -->
 
	</script>
</form>

Then style your button accordingly with your css,

/* buy button */
#buyButtonRight{
	width:16px;
	height:44px;
	display:block;
	position:absolute;
	top:0;
	right:0;
	cursor:pointer;
	}
 
#buyButtonLeft{
	width:16px;
	height:44px;
	display:block;
	position:absolute;
	top:0;
	left:0;
	cursor:pointer;
	}
 
#buyButtonMid{
	height:24px;
	min-width:20px;
	/*max-width:300px;*/
	padding:13px 20px 7px 20px;
	display:block;
	position:relative;
	float:left;
	cursor:pointer;
	color:#ffffff; 
	font-size:13px; 
	text-decoration:none;
	}
 
 
/* normal */
#buyButtonLeft{background:url('images/add2bas_left.jpg');}	
#buyButtonMid{background:url('images/add2bas_mid.jpg');}	
#buyButtonRight{background:url('images/add2bas_right.jpg');}
 
/* hover */
#buyButtonMid:hover #buyButtonLeft{background:url('images/add2bas_left_hover.jpg');}
#buyButtonMid:hover {background:url('images/add2bas_mid_hover.jpg');}
#buyButtonMid:hover #buyButtonRight{background:url('images/add2bas_right_hover.jpg');}
 
/* click */
#buyButtonMid:active #buyButtonLeft{background:url('images/add2bas_left_click.jpg') no-repeat;}
#buyButtonMid:active {background:url('images/add2bas_mid_click.jpg');}
#buyButtonMid:active #buyButtonRight{background:url('images/add2bas_right_click.jpg');}

Job done ;)

VN:F [1.9.9_1125]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.9_1125]
Rating: 0 (from 0 votes)
Leave a Comment more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!