Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
217 additions
and
141 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
} | ||
|
||
|
||
|
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |