Override and multiple routes for same route


hi! community !

my question is…

in my old route:

    $app->get('/', function ($request, $response, $args) {

        // Render index view
        $res = $this->renderer->render($response, 'site/index.php', $args);


            $app->get('/profile/{user_name}', function ($request, $response, $args) {
                    $res = $this->renderer->render($response, 'site/username_template.php', $args);

this was working without problem…

but in the new requeriment… they want…

$app->get('/{user_name}', function ($request, $response, $args) {
            $res = $this->renderer->render($response, 'site/username_template.php', $args);

without prefix . . .

but the also want . . .

$app->get('/{user_name}/orders', function ($request, $response, $args) {
            $res = $this->renderer->render($response, 'site/orders_template.php', $args);

 $app->get('/{user_name}/products', function ($request, $response, $args) {
                $res = $this->renderer->render($response, 'site/products_template.php', $args);


$app->get('/cart', function ($request, $response, $args) {
                    $res = $this->renderer->render($response, 'site/cart_template.php', $args);

$app->get('/cart/detail', function ($request, $response, $args) {
                    $res = $this->renderer->render($response, 'site/cart_details_template.php', $args);

How can we create these rules and considering that the {user_name} is dynamic, is in the database the user UNIQUE user, how to do so that you can interpret that and the routes ( /cart and /cart/detail )at the same time



The first matching route wins. Just put the routes /cart and /cart/detail above the /{user_name}/orders and /{user_name}/products. Make sure that the /{user_name} is the last one in your routing definition file.

Example order:

/{user_name} <-- the last route


$app->get('/', function (\Slim\Http\Request $request, \Slim\Http\Response $response) {
    return $response->getBody()->write('home');

$app->get('/{user_name}/review', function (\Slim\Http\Request $request, \Slim\Http\Response $response, $args = []) {
    return $response->getBody()->write('review:' . $args['user_name']);

// the last route
$app->get('/{user_name}', function (\Slim\Http\Request $request, \Slim\Http\Response $response, $args = []) {
    return $response->getBody()->write('username:' . $args['user_name']);


thank you for you answer odan . . .

but… the problem is . . …

i have a cart_route.php, in this file… i have any route for cart ( /cart , /cart/detail, /cart/delete … and others . . . )

in other file user_route.php the route for users… example ( /{user_name} , /{user_name}/dashboard , /{user_name}/conferences , /{user_name}/collections/details … where this variable is dinamyc and a unique in the data base ).

when i defined and test in user_route.php by example /{user_name} , my route in cart_route.php, ( /cart , /cart/detail, /cart/delete /cart/detele/{id_product} . ) and products_route.php ( /products/list , /product/detail/{id_product} ) Doesn’t work


when you say . …

The first matching route wins. . … .

is for order my order load of my controllers ? O.o

actually, my order is:



I didn’t mean the order of the controllers, but the order of the routing definitions. Make sure that the routing definitions are defined in the correct order.


hi odan… i fox the post…

i have the orders routes…

but a tried to change the order of this