mirror of
https://github.com/dannyvankooten/AltoRouter.git
synced 2025-08-04 15:37:45 +02:00
Updated readme, added more inline documentation.
This commit is contained in:
@@ -1,15 +1,26 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
class AltoRouter {
|
class AltoRouter {
|
||||||
|
|
||||||
private $routes = array();
|
private $routes = array();
|
||||||
private $namedRoutes = array();
|
private $namedRoutes = array();
|
||||||
private $basePath = '';
|
private $basePath = '';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the base path.
|
||||||
|
* Useful if you are running your application from a subdirectory.
|
||||||
|
*/
|
||||||
public function setBasePath($basePath) {
|
public function setBasePath($basePath) {
|
||||||
$this->basePath = $basePath;
|
$this->basePath = $basePath;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Map a route to a target
|
* Map a route to a target
|
||||||
|
*
|
||||||
|
* @param string $method One of 4 HTTP Methods, or a pipe-separated list of multiple HTTP Methods (GET|POST|PUT|DELETE)
|
||||||
|
* @param string $route The route regex, custom regex must start with an @. You can use multiple pre-set regex filters, like [i:id]
|
||||||
|
* @param mixed $target The target where this route should point to. Can be anything.
|
||||||
|
* @param string $name Optional name of this route. Supply if you want to reverse route this url in your application.
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
public function map($method, $route, $target, $name = null) {
|
public function map($method, $route, $target, $name = null) {
|
||||||
|
|
||||||
@@ -25,12 +36,18 @@ class AltoRouter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reversed routing
|
* Reversed routing
|
||||||
*
|
*
|
||||||
* Generate the URL for a named route. Replace regexes with supplied parameters
|
* Generate the URL for a named route. Replace regexes with supplied parameters
|
||||||
|
*
|
||||||
|
* @param string $routeName The name of the route.
|
||||||
|
* @param array @params Associative array of parameters to replace placeholders with.
|
||||||
|
* @return string The URL of the route with named parameters in place.
|
||||||
*/
|
*/
|
||||||
public function generate($routeName, array $params = array()) {
|
public function generate($routeName, array $params = array()) {
|
||||||
|
|
||||||
@@ -61,6 +78,9 @@ class AltoRouter {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Match a given Request Url against stored routes
|
* Match a given Request Url against stored routes
|
||||||
|
* @param string $requestUrl
|
||||||
|
* @param string $requestMethod
|
||||||
|
* @return array|boolean Array with route information on success, false on failure (no match).
|
||||||
*/
|
*/
|
||||||
public function match($requestUrl = null, $requestMethod = null) {
|
public function match($requestUrl = null, $requestMethod = null) {
|
||||||
|
|
||||||
@@ -141,7 +161,7 @@ class AltoRouter {
|
|||||||
|
|
||||||
|
|
||||||
if(($match == true || $match > 0)) {
|
if(($match == true || $match > 0)) {
|
||||||
|
|
||||||
if($params) {
|
if($params) {
|
||||||
foreach($params as $key => $value) {
|
foreach($params as $key => $value) {
|
||||||
if(is_numeric($key)) unset($params[$key]);
|
if(is_numeric($key)) unset($params[$key]);
|
||||||
|
@@ -18,10 +18,10 @@ $router = new AltoRouter();
|
|||||||
$router->setBasePath('/AltoRouter');
|
$router->setBasePath('/AltoRouter');
|
||||||
|
|
||||||
// mapping routes
|
// mapping routes
|
||||||
$router->map('GET|POST','/', 'home#index', array('name' => 'home'));
|
$router->map('GET|POST','/', 'home#index', 'home');
|
||||||
$router->map('GET','/users/', array('c' => 'UserController', 'a' => 'ListAction'));
|
$router->map('GET','/users/', array('c' => 'UserController', 'a' => 'ListAction'));
|
||||||
$router->map('GET','/users/[i:id]', 'users#show', array('name' => 'users_show'));
|
$router->map('GET','/users/[i:id]', 'users#show', 'users_show');
|
||||||
$router->map('POST','/users/[i:id]/[delete|update:action]', 'usersController#doAction', array('name' => 'users_do'));
|
$router->map('POST','/users/[i:id]/[delete|update:action]', 'usersController#doAction', 'users_do');
|
||||||
|
|
||||||
|
|
||||||
// reversed routing
|
// reversed routing
|
||||||
|
Reference in New Issue
Block a user