Proszę bardzo : )
sammy.php
<?php
/**
* Sammy - A bare-bones PHP version of the Ruby Sinatra framework.
*
* @version 1.0
* @author Dan Horrigan
* @license MIT License
* @copyright 2010 Dan Horrigan
*/
function get($route, $callback) {
Sammy::process($route, $callback, 'GET');
}
function post($route, $callback) {
Sammy::process($route, $callback, 'POST');
}
function put($route, $callback) {
Sammy::process($route, $callback, 'PUT');
}
function delete($route, $callback) {
Sammy::process($route, $callback, 'DELETE');
}
function ajax($route, $callback) {
Sammy::process($route, $callback, 'XMLHttpRequest');
}
class Sammy {
public static $route_found = false;
public $uri = '';
public $segments = '';
public $method = '';
public $format = '';
public static function instance
() {
if( $instance === null ) {
$instance = new Sammy;
}
return $instance;
}
public static function run
() { if( !self::$route_found ) {
echo 'Route not defined!'; }
}
public static function process
($route, $callback, $type) { $sammy = self::instance();
// Check for ajax
if( $type == 'XMLHttpRequest' )
$sammy->method = isset($_SERVER['HTTP_X_REQUESTED_WITH']) ?
$_SERVER['HTTP_X_REQUESTED_WITH'] : 'GET';
if( self::$route_found || (!preg_match('@^'.$route.'(?:\.(\w+))?$@uD', $sammy->uri, $matches) || $sammy->method != $type) ) { return false;
}
// Get the extension
$extension = $matches[count($matches)-1
];
if( $extension_test == '.' . $extension )
$sammy->format = $extension;
self::$route_found = true;
}
public function __construct() {
$this->uri = $this->get_uri();
$this->method = $this->get_method();
}
public function segment($num) {
$num--;
// Remove the extension
$this->segments[$num] = isset($this->segments[$num]) ?
rtrim($this->segments[$num], '.' . $this->format) : null;
return isset($this->segments[$num]) ?
$this->segments[$num] : null; }
protected function get_method() {
return isset($_SERVER['REQUEST_METHOD']) ?
$_SERVER['REQUEST_METHOD'] : 'GET'; }
protected function get_uri($prefix_slash = true) {
if( isset($_SERVER['PATH_INFO']) ) { $uri = $_SERVER['PATH_INFO'];
}elseif( isset($_SERVER['REQUEST_URI']) ) { $uri = $_SERVER['REQUEST_URI'];
if( strpos($uri, $_SERVER['SCRIPT_NAME']) === 0
) { }elseif( strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0
) { }
// This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct
// URI is found, and also fixes the QUERY_STRING server var and $_GET array.
if( strncmp($uri, '?/', 2
) === 0
) { }
$uri = $parts[0];
$_SERVER['QUERY_STRING'] = $parts[1];
}else {
$_SERVER['QUERY_STRING'] = '';
}
}else {
// Couldn't determine the URI, so just return false
return false;
}
// Do some final cleaning of the URI and return it
}
public function format($name, $callback) {
$sammy = self::instance();
if( !empty($sammy->format) && $name == $sammy->format ) else
return false;
}
}
$sammy = Sammy::instance();
router.php
<?php
class Map {
public static function get
($route, $callback) { Sammy::process($route, 'GET');
}
public static function post
($route, $callback) { Sammy::process($route, 'POST');
}
public static function put
($route, $callback) { Sammy::process($route, 'PUT');
}
public static function delete
($route, $callback) { Sammy::process($route, 'DELETE');
}
public static function ajax
($route, $callback) { Sammy::process($route, 'XMLHttpRequest');
}
public static function dispatch
() { // runs when find a matching route
// find the controller
// find the action
// include the app_controller
// include the matching route controller
// run the matching action
// include the view file
}
}
index.php
<?php
//for use in development mode
define ('APP_PATH', BASE_PATH
. 'app/'); //
//echo BASE_PATH;
include BASE_PATH . 'libraries/core.php';
Jestem pewien że kod jest na 99% dobrze napisany. Sprawdzałem do 4 nad ranem porównując z filmem oraz z gotowcem w zipie rozwiązaniem partu I .