Override and multiple routes for same route


#1

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);
    });

and

            $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);
            });

and

$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


#2

Hi!

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:

/
/cart
/cart/detail
/{user_name}/orders
/{user_name}/products
/{user_name} <-- the last route

Example

$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']);
});

#3

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


#4

when you say . …

The first matching route wins. . … .

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

actually, my order is:

/MYSERVER/XAMPP/xamppfiles/htdocs/MYSITE/core/app/site/controllers/artistController.php
/MYSERVER/XAMPP/xamppfiles/htdocs/MYSITE/core/app/site/controllers/artworksController.php
/MYSERVER/XAMPP/xamppfiles/htdocs/MYSITE/core/app/site/controllers/cartController.php
/MYSERVER/XAMPP/xamppfiles/htdocs/MYSITE/core/app/site/controllers/checkoutController.php
/MYSERVER/XAMPP/xamppfiles/htdocs/MYSITE/core/app/site/controllers/colectionController.php
/MYSERVER/XAMPP/xamppfiles/htdocs/MYSITE/core/app/site/controllers/orderController.php
/MYSERVER/XAMPP/xamppfiles/htdocs/MYSITE/core/app/site/controllers/paymentController.php
/MYSERVER/XAMPP/xamppfiles/htdocs/MYSITE/core/app/site/controllers/productController.php
/MYSERVER/XAMPP/xamppfiles/htdocs/MYSITE/core/app/site/controllers/userContoller.php


#5

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.


#6

hi odan… i fox the post…

i have the orders routes…

but a tried to change the order of this