Giter Club home page Giter Club logo

router's Introduction

Router

<?php
class Router {
	/**
	* The console handler is invoked whenever appropriate
	* @param handler - the console handler
	* Throws RoutingException if the handler is not callable
	*/
	public function setConsole(Callable handler);
	
	/**
	* The default handler is invoked when a web request matches no other handler
	* @param handler - the default web request handler
	* Throws RoutingException if the handler is not callable
	*/
	public function setDefault(Callable handler);
	
	/**
	* @param method   - case insensitive request method
	* @param uri	  - regexp for request uri
	* @param handler  - route handler
	* Throws RoutingException if the handler is not callable
	*/
	public function addRoute(string method, string uri, Callable handler);
	
	/**
	* @param method   - route
	* Throws RoutingException upon failure to manipulate the route appropriately
	*/
	public function addRoute(Route route);
	
	/**
	* Sets the request, overriding detection on ::route
	* @param method - method for the request
	* @param uri    - uri for the request
	* Throws RoutingException upon failure to find suitable handler
	*/
	public function setRequest(string method, string uri);
	
	/**
	* Sends a Location: header and finishes the request
	* @param location - the location uri
	* @param code     - ROUTER_DIRECT_TEMP or ROUTER_REDIRECT_PERM
	*/
	public function redirect(string location [, integer code = ROUTER_REDIRECT_TEMP]);
	
	/**
	* Reroutes the current request without redirecting the browser
	* @param method - target method
	* @param uri    - target uri
	* Note superglobals are not touched, only the correct handler is invoked
	* Throws RoutingException upon failure to find suitable handler
	*/
	public function reroute(string method, string uri);
	
	/**
	* Invoke the most suitable handler for the current request
	* Throws RoutingException upon failure to find suitable handler
	*/
	public function route(void);
}
?>

Route

<?php
interface Route {
	/**
	* Should return get/put/patch/post etc
	* @return string
	*/
	public function getMethod();
	
	/**
	* Should return a valid regex pattern describing request
	* @return string
	*/
	public function getURI();
	
	/**
	* Will be invoked for appropriate requests
	* @param patterns - groups array
	* @return boolean
	*/
	public function handle($patterns);
}
?>

Constants

For redirection:

  • ROUTER_REDIRECT_TEMP
  • ROUTER_REDIRECT_PERM

Hello World

<?php
try {
	$router = new Router();
	$router->setDefault(function(){
		printf("Hello World\n");
		return true;
	})
	->route();
} catch(RoutingException $ex) {
	printf(
		"<pre>Exception: %s\n</pre>", (string)$ex);
}
?>

Hello Console & Web

<?php
try {
	$router = new Router();
	$router
		->setDefault(function(){
			printf("Hello Web User\n");
			return true;
		})
		->setConsole(function(){
			printf("Hello Console User\n");
			return true;
		})
		->route();
} catch(RoutingException $ex) {
	printf(
		"<pre>Exception: %s\n</pre>", (string)$ex);
}
?>

Hello Objects

<?php
class BlogRoute implements Route {
	/* always return a valid method string */
	public function getMethod() 		{ return "get"; }
	/* always return a valid regex pattern string */
	public function getURI()    		{ return "~/blog/?(.*)?~"; }
	/* always return a boolean value */
	public function handle($patterns)	{ return true; }
}

class DefaultRoute implements Route {
	/* always return a valid method string */
	public function getMethod() 		{ return "get"; }
	/* always return a valid regex pattern string */
	public function getURI()    		{ return "~/(.*)~"; }
	/* always return a boolean value */
	public function handle($patterns)	{ return true; }
}

try {
	$router = new Router();
	$router
		->addRoute(new BlogRoute())
		->addRoute(new DefaultRoute())
		->setRequest("get", "/whatever")
		->route();
} catch(RoutingException $ex) {
	printf(
		"<pre>Exception: %s\n</pre>", (string)$ex);
}
?>

router's People

Contributors

krakjoe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.