// or somewhat more complicated
$router->map('/users/:id/edit/', array('controller' => 'SomeController', 'action' => 'someAction'), array('methods' => 'GET,PUT', 'name' => 'users_edit'));
+ // You can even specify closures as the Route's target
+ $router->map('/hello/:name', function($name) { echo "Hello $name."; });
+
// match current request URL & http method
$target = $router->matchCurrentRequest();
var_dump($target);
*/
private $filters = array();
+ /**
+ * Array containing parameters passed through request URL
+ * @var array
+ */
+ private $params = array();
+
public function getUrl() {
return $this->url;
}
public function setUrl($url) {
+ $url = (string) $url;
+
+ // make sure that the URL is suffixed with a forward slash
+ if(substr($url,-1) !== '/') $url .= '/';
+
$this->url = $url;
}
return "(\w+)";
}
+ public function getParameters() {
+ return $this->parameters;
+ }
+
+ public function setParameters(array $parameters) {
+ $this->parameters = $parameters;
+ }
+
$router = new Router();
$router->setBasePath('/PHP-Router');
-// maps / to controller 'users' and method 'index'.
$router->map('/', 'someController:indexAction', array('methods' => 'GET'));
$router->map('/users/:id/edit/', 'users#edit', array('methods' => 'GET', 'name' => 'users_edit'));
$router->map('/contact/',array('controller' => 'someController', 'action' => 'contactAction'), array('name' => 'contact'));
$router->map('/users/','users#create', array('methods' => 'POST', 'name' => 'users_create'));
$router->map('/users/','users#list', array('methods' => 'GET', 'name' => 'users_list'));
+$route = $router->matchCurrentRequest();
+
?><h3>Current URL & HTTP method would route to: </h3>
-<pre><?php var_dump($router->matchCurrentRequest()); ?></pre>
+<?php if($route) { ?>
+ <pre><?php var_dump($route->getTarget()); ?></pre>
+ <pre><?php var_dump($route->getParameters()); ?></pre>
+<?php } else { ?>
+ <pre>No route matched.</pre>
+<?php } ?>
<h3>Try out these URL's.</h3>
<p><a href="<?php echo $router->generate('users_edit', array('id' => 5)); ?>"><?php echo $router->generate('users_edit', array('id' => 5)); ?></a></p>