Skip to content

Loading…

Added New Route Options as per Comment by ross #1

Merged
merged 2 commits into from

3 participants

@gmanricks

New Route Adding Options

I added some new ways to add routes as per a comment left by ross on the NetTuts article. These changes are backwards compatible, and the old method will still work.

The Options:

1) Key - Value (The Original Way)

$routes = array(
    '/' => '',
    '/test/:title' => 'Main:test@get'
);

2) Array (Route, Action)

This is from the comment on NetTuts, it is in-order that you can have multiple routes pointing to the same endpoint. (e.g. different methods)

$routes = array(
    array('/home', 'Main:displayhome@get'),
    array('/home', 'Main:addComment@post')
);

3) Key - Array

This is a mix of the two, providing a slightly nicer syntax.

$routes = array(
    '/users' => array(
        'get'     => 'UsersController:displayUsers',
        'post'   => 'UsersController:addUser',
        'delete' => 'UsersController:removeUser'
    )
);

And of course the 3 different options can be mixed and matched so your route array can look like:

$routes = array(
    '/' => '',
    '/test/:title' => "Main:test@get",

    array('/arr', "Main:index@get"),

    '/demo' => array(
        "get" => "Main:test2",
        "post" => "Main:test3"
    )
);
@JeffreyWay JeffreyWay merged commit 8ac5a28 into NETTUTS:master
@ksbomj

Hi guys! How to fetch request arguments in code?

My route:

$routes = array(
    '/page/:id' => 'Page:index@get'
);

in controller

$this->request->get('id');

dosen't work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 28, 2013
  1. @gmanricks
  2. @gmanricks
Showing with 44 additions and 11 deletions.
  1. +11 −0 Controller/Main.php
  2. BIN public/.index.php.swp
  3. +8 −1 public/index.php
  4. +25 −10 vendor/NetTuts/Router.php
View
11 Controller/Main.php
@@ -14,6 +14,17 @@ public function test($title)
$this->render("test", array("title" => $title, "name" => "Test"));
}
+ public function test2()
+ {
+ $this->render("test", array("title" => "GET", "name" => "Test 2"));
+ }
+
+ public function test3()
+ {
+ $this->render("test", array("title" => "POST", "name" => "Test 3"));
+ }
+
+
public function notFound()
{
$this->render("error", array(), 404);
View
BIN public/.index.php.swp
Binary file not shown.
View
9 public/index.php
@@ -6,7 +6,14 @@
$routes = array(
'/' => '',
- '/test/:title' => "Main:test@get"
+ '/test/:title' => "Main:test@get",
+
+ array('/arr', "Main:index@get"),
+
+ '/demo' => array(
+ "get" => "Main:test2",
+ "post" => "Main:test3"
+ )
);
$router->addRoutes($routes);
View
35 vendor/NetTuts/Router.php
@@ -18,20 +18,35 @@ public function __construct()
public function addRoutes($routes)
{
foreach ($routes as $route => $path) {
-
- $method = "any";
-
- if (strpos($path, "@") !== false) {
- list($path, $method) = explode("@", $path);
+
+ if (is_string($route)) {
+ if (is_array($path)) {
+ foreach($path as $method => $action) {
+ $this->addRoute($route, $action . "@" . $method);
+ }
+ } else {
+ $this->addRoute($route, $path);
+ }
+ } else {
+ $this->addRoute($path[0], $path[1]);
}
- $func = $this->processCallback($path);
-
- $r = new \Slim\Route($route, $func);
- $r->setHttpMethods(strtoupper($method));
+ }
+ }
- array_push($this->routes, $r);
+ protected function addRoute($route, $pathStr) {
+ $method = "any";
+
+ if (strpos($pathStr, "@") !== false) {
+ list($pathStr, $method) = explode("@", $pathStr);
}
+
+ $func = $this->processCallback($pathStr);
+
+ $r = new \Slim\Route($route, $func);
+ $r->setHttpMethods(strtoupper($method));
+
+ array_push($this->routes, $r);
}
protected function processCallback($path)
Something went wrong with that request. Please try again.