Work
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.
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)
wordpress comment count as a button
by Adrian on Jul.30, 2009, under Programming, wordpress, Work
Styling the wordpress comment count as a button/icon etc can be tricky because the normal call to the comments
1 | comments_popup_link(__('Comments (0)'), __('Comments (1)'), __('Comments (%)')); |
formats the output as an anchor point with the correct link, alt tags etc already set up.
So in order to intercept this and then add you own anchor point with divs/imagery etc you need to make a custom request
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | // SQL $SQL = "SELECT COUNT(*) AS Count FROM $wpdb->comments WHERE comment_approved='1'"; $SQL.= "AND comment_post_ID='".intval($post->ID)."'"; // Sort the array into a string prefixed with 'Comments ' list($comments) = $wpdb->get_results($SQL, ARRAY_A); $comments='Comments ('.$comments['Count'].')'; // generate the alt and title tags $alt = 'Comment on '.$post->post_title; // echo the button if this is not a single and not a page if (!is_single() && !is_page()) { // anchor open echo '<a href="'.get_permalink($post->ID).'" alt="'.$alt.'" title="'.$alt.'">'; // complex formatting rules for the comment pop up, go here echo $comments; // anchor close echo '</a>'; } |
The above does exactly the same, formats the link etc, but now you can access the anchor point directly and format it your own way.
General discussion RSS Feed