Skip to content

Commit

Permalink
Added MIT license and basic readme
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmoss committed Jan 5, 2012
1 parent 6abd595 commit b2de40c
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 141 deletions.
8 changes: 8 additions & 0 deletions LICENSE
@@ -0,0 +1,8 @@
The MIT License (MIT)
Copyright (c) 2012 James Moss, http://jamesmoss.co.uk

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
50 changes: 50 additions & 0 deletions README.md
@@ -0,0 +1,50 @@
# Routa

A RESTful, flexible MVC router component for PHP 5.3+. Declare rules which route HTTP requests to a controller.

## Do we need another router?

There's loads of router components so why do we need another? Many of the existing ones are tightly coupled, poorly written. Routa is built with the following in mind:

- RESTful
- Loosely coupled
- Inspired by Ruby on Rails
- Compatible with PHP >= 5.3
- Prefers convention over configuration
- Flexible and extendable
- Automated test suite

## Installation

Routa adheres to the [PSR-0](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md) spec. If you have a compatible autoloader place the contents of `lib` in your vendor directory.

## Basic usage

Within your code create a new instance of the router class.

$router = new JamesMoss\Routa\Router;

This approach automatically uses the current page URL and HTTP method from the $_SERVER super global, it's possible to override this which we'll cover later.

Next add some routes to map URLs to controller names/actions:

$router
->add('/login', 'Account#login')
->add('/profile/:id', 'User#view')
->add('/profile/:id/edit', 'User#edit')
->add('/search/*', 'Products#search');

Once all your routes have been declared match your request against them.

if($result = $router->match()) {
// Assuming the url is /profile/432
var_dump($result->controller); // returns 'User'
var_dump($result->action); // returns 'view'
var_dump($result->params); // returns ['id' => 432]
// Load your controller…
} else {
// Display your 404 page.
}



16 changes: 8 additions & 8 deletions lib/JamesMoss/Routa/Router.php
Expand Up @@ -69,18 +69,18 @@ public function add($route, $action = null)
$route = new Routes\Basic($route, $action);
}

$this->_routes[] = $route;
$route->add($this->_routes);

return $this;
}

/**
* Matches a route against a request
*
* @author James Moss
* @param $param
* @return null
*/
* Matches a route against a request
*
* @author James Moss
* @param $param
* @return null
*/
public function match()
{
// Loop over the routes and check each one
Expand All @@ -92,7 +92,7 @@ public function match()

return false;
}

protected function isValidHttpMethod($method)
{
$allowed = explode(',', 'GET,POST,PUT,DELETE,HEAD,TRACE,OPTIONS,CONNECT,PATCH');
Expand Down
134 changes: 1 addition & 133 deletions lib/JamesMoss/Routa/Routes/Basic.php
Expand Up @@ -9,144 +9,12 @@
*
* @author James Moss <email@jamesmoss.co.uk>
*/
class Basic
class Basic extends Route
{
/**
* Regex used to extract simple tokens from routes
*
* @var string
*/
const PARAMS_REGEX = '/\:([a-zA-Z0-9\-]+)/';

/**
* The URL to use
*
* @var string
*/
protected $_url = null;

/**
* The name of the controller to return on a match
*
* @var string
*/
protected $_controller = null;

/**
* The compiled regex URL
*
* @var string
*/
protected $_compiledUrl = '';

/**
* Route parameters (if any)
*
* @var array
*/
protected $_params = array();

/**
* Tokens
*
* @var array
*/
protected $_tokens = array();

/**
* Constructor
*
* @author James Moss
* @param $url
* @param $controller
*/
public function __construct($url, $controller)
{
$this->_url = $url;
$this->_controller = $controller;
$this->_compile();
}

/**
* Takes a request and matches it against this route
*
* @author James Moss
* @param $request
* @return boolean
*/
public function match($request)
{
$url = $request->url;

if($this->_url === $url || (count($this->_tokens) && $this->_parseParams($url))) {
return true;
}

return false;
}

/**
* Return the controller name
*
* @author James Moss
* @param $param
* @return string
*/
public function getControllerName()
{
return $this->_controller;
}

/**
* returns the params
*
* @author James Moss
* @param $param
* @return array
*/
public function getParams()
{
return $this->_params;
}

/**
* Turns a human readable route into a regex to match a URL
*
* @author James Moss
*/
protected function _compile()
{
$tokens = &$this->_tokens; // you cant pass class properties into closures in PHP <= 5.3
$this->_compiledUrl = str_replace('/', '\/', $this->_url);
$this->_compiledUrl = preg_replace_callback(self::PARAMS_REGEX, function($matches) use(&$tokens) {
$tokens[] = $matches[1];
return '([^\/]+?)';
}, $this->_compiledUrl);

$this->_compiledUrl = '/^'.$this->_compiledUrl.'$/';
}

/**
* Combines the URL and tokens to make params
*
* @author James Moss
* @param $url
* @return boolean
*/
protected function _parseParams($url)
{
if(count($this->_params)) {
return true;
}

if(preg_match_all($this->_compiledUrl, $url, $matches)) {
array_shift($matches);
foreach($matches as $i => $match) {
$this->_params[$this->_tokens[$i]] = $match[0];
}
return true;
}

return false;
}
}
150 changes: 150 additions & 0 deletions lib/JamesMoss/Routa/Routes/Route.php
@@ -0,0 +1,150 @@
<?php

namespace JamesMoss\Routa\Routes\Route;

/**
* Route
*
* Describes a URL -> action match
*
* @author James Moss <email@jamesmoss.co.uk>
*/
abstract class Route
{
/**
* The URL to use
*
* @var string
*/
protected $_url = null;

/**
* The name of the controller to return on a match
*
* @var string
*/
protected $_controller = null;

/**
* The compiled regex URL
*
* @var string
*/
protected $_compiledUrl = '';

/**
* Route parameters (if any)
*
* @var array
*/
protected $_params = array();

/**
* Tokens
*
* @var array
*/
protected $_tokens = array();

/**
* Constructor
*
* @author James Moss
* @param $url
* @param $controller
*/
public function __construct($url, $controller)
{
$this->_url = $url;
$this->_controller = $controller;
$this->_compile();
}

/**
* Takes a request and matches it against this route
*
* @author James Moss
* @param $request
* @return boolean
*/
public function match($request)
{
$url = $request->url;

if($this->_url === $url || (count($this->_tokens) && $this->_parseParams($url))) {
return true;
}

return false;
}

/**
* Return the controller name
*
* @author James Moss
* @param $param
* @return string
*/
public function getControllerName()
{
return $this->_controller;
}

/**
* returns the params
*
* @author James Moss
* @param $param
* @return array
*/
public function getParams()
{
return $this->_params;
}

public function add(&$routes)
{
$routes[] = $this;
}

/**
* Turns a human readable route into a regex to match a URL
*
* @author James Moss
*/
protected function _compile()
{
$tokens = &$this->_tokens; // you cant pass class properties into closures in PHP <= 5.3
$this->_compiledUrl = str_replace('/', '\/', $this->_url);
$this->_compiledUrl = preg_replace_callback(self::PARAMS_REGEX, function($matches) use(&$tokens) {
$tokens[] = $matches[1];
return '([^\/]+?)';
}, $this->_compiledUrl);

$this->_compiledUrl = '/^'.$this->_compiledUrl.'$/';
}

/**
* Combines the URL and tokens to make params
*
* @author James Moss
* @param $url
* @return boolean
*/
protected function _parseParams($url)
{
if(count($this->_params)) {
return true;
}

if(preg_match_all($this->_compiledUrl, $url, $matches)) {
array_shift($matches);
foreach($matches as $i => $match) {
$this->_params[$this->_tokens[$i]] = $match[0];
}
return true;
}

return false;
}
}

0 comments on commit b2de40c

Please sign in to comment.