Routing

Routes

The routing makes use of the PHP PATH_INFO, which can be seen behind the index.php of the appropriate controller and the function.

// http://localhost/PLAIN_PHP/index.php/Manual/routes

The route above should be in the Manual controller and calls the routes function.

To create a link to this page, a controller function is provided. Below is an excerpt from the page menu:

<li><a href="<?php echo Manual::linkTo("routes"); ?>">Ordnerstruktur und Routing</a></li>

Passing parameters

To pass parameters to your controller, you can add any amount of parameters to your route. A common case is a database id to fetch some content.

These additional parameters are added straight to the end of your default route

Users::linkTo("showTODOList", 3, 9);
// will build to http://host/index.php/Users/showTODOList/3/9

Your controller can receive these values by just grab them in the order they are in the route

// User controller
public static function showTODOList($userId, $listId){ ... }

Cool PHP feature: you can use the default values with your controller parameters of course!

// User controller
public static function showTODOList($userId, $listId = false){
    if(!$listId){
        //fetch first one
    }
}

Custom Routing

It is not difficult to note that the URL of the current page does not match the expected route:

// http://localhost/PLAIN_PHP/index.php/CUSTOMROUTING

If you want to hide your controller names or rearrange the values to match other APIs, you can create a whole new route

It is possible to specify your own route for each controller function in lib/config/routes.php.

//config/routes.php
$_ROUTES = array(
    "/CUSTOMROUTING" => "Manual::routes",
    "/debug/{value}/test/{yay}" => "MyAwsomeController::awesomerFunction",
    "/goodbye/{userId}" => "Users::delete",
);  
    
Users::linkTo("delete", 8);
// returns http://localhost/PHP_PLAIN/index.php/goodbye/8

MyAwsomeController::linkTo("awesomerFunction", "33", "AWESOME"); // or
MyAwsomeController::linkTo("awesomerFunction", array("33", "AWESOME"));
// returns http://localhost/PHP_PLAIN/index.php/debug/33/test/AWESOME

//in the Users controller
public static delete($id){
    //$id = 8
}

//in the MyAwsomeController controller
public static awesomerFunction($id, $test){
    //$id = 33, $test = "AWESOME"
}

Content Negotiation

In all of the examples above, we completely ignore the REQUEST_METHOD. Every route reacts independent from the method to the matching URL.

If we want to build a nice API, we can limit our routes to specific request methods. So we can specify one route with 3 diffrent targets, depending on the request method.

//config/routes.php
$_ROUTES = array(
    "(GET)/profile/{id}" => "Profile::show",
    "(POST)/profile/{id}" => "Profile::update",
    "(DELETE)/profile/{id}" => "Profile::delete"
)