Created
May 1, 2012 20:16
-
-
Save mbernson/2571050 to your computer and use it in GitHub Desktop.
Prul - unfinished URL router in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<title>Prul</title> | |
<h1><a href="/">Prul</a></h1> | |
<ul> | |
<li><a href="/">Index</a></li> | |
<li><a href="/test/">Test</a></li> | |
</ul> | |
<?php | |
include 'lib/Prul.php'; | |
Prul::get( '/' )->respond(function(){ // Respond with an anonymous function. | |
return '<p>Homepage.</p>'; | |
}); | |
// This will call the function "submit_form", if it exists. | |
Prul::post( '/hallo' )->respond( 'submit_form' ); | |
Prul::get( '/page/:id' )->respond( function( $page_id ){ | |
} ); | |
// These won't do anything. | |
Prul::get( '' ); | |
Prul::get( '/whut' ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include_once 'PrulRoute.php'; | |
include_once 'PrulException.php'; | |
class Prul | |
{ | |
protected static $routes = array(); | |
const GET = 'GET'; | |
const POST = 'POST'; | |
const PUT = 'PUT'; | |
const DELETE = 'DELETE'; | |
private function __construct(){} | |
public static function getInstance() | |
{ | |
if( ! Prul::$instance instanceof self ) | |
{ | |
Prul::$instance = new self(); | |
} | |
return self::$instance; | |
} | |
public static function getRoute( $url ) | |
{ | |
return new PrulRoute( $url ); | |
} | |
public static function isRequest( $request ) | |
{ | |
if( $_SERVER['REQUEST_METHOD'] === $request ) | |
{ | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
/** | |
* is function. | |
* Alias to isRequest. | |
* @access public | |
* @static | |
* @param mixed $r | |
* @return void | |
*/ | |
public static function is( $r ) | |
{ | |
return self::isRequest( $r ); | |
} | |
public static function matches( $url ) | |
{ | |
if( $_SERVER['REQUEST_URI'] === $url ) | |
{ | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
private static function addRoute( PrulRoute $route ) | |
{ | |
$this->routes[ $route->getURL() ] = $route; | |
} | |
/* | |
* | |
*/ | |
public static function get( $url ) | |
{ | |
$r = self::getRoute( $url ); | |
$r->setMethod( self::GET ); | |
self::$routes[] = $r; | |
return $p; | |
} | |
public static function post( $url ) | |
{} | |
public static function put( $url ) | |
{} | |
public static function delete( $url ) | |
{} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class PrulException extends Exception | |
{} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include_once 'PrulException.php'; | |
/** | |
* PrulRoute | |
*/ | |
class PrulRoute | |
{ | |
private $_url = ''; | |
private $_method = Prul::GET; | |
public function __construct( $url = '' ) | |
{ | |
$this->_url = $url; | |
} | |
public function respond( $something = null ) | |
{ | |
if( Prul::is( $this->_method ) and Prul::matches( $this->_url ) ) | |
{ | |
$this->output( $something ); | |
} | |
} | |
private function output( $something ) | |
{ | |
if( is_callable( $something ) ) | |
{ | |
return call_user_func( $something ); | |
} | |
elseif( is_string( $something ) ) | |
{ | |
echo $something; | |
} | |
return null; | |
} | |
/** | |
* setMethod | |
* Sets the HTTP request method for this route. | |
* | |
* @access public | |
* @param $method | |
* @return void | |
*/ | |
public function setMethod( $method ) | |
{ | |
$this->_method = $method; | |
} | |
public function getMethod() | |
{ | |
return $this->_method; | |
} | |
public function setURL( $url ) | |
{ | |
$this->_url = $url; | |
} | |
public function getURL() | |
{ | |
return $this->_url; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment