How to add default route condition in slim 4?

66 views Asked by At

In Slim 2, we have a static route method called 'setDefaultConditions' where we can pre-define route names with regular expressions.

Route::setDefaultConditions(
            array(
              
                'product_id'         => Regex::VALID_PRODUCT_ID,
                'id'                 => Regex::VALID_NUM,
                'cart_id'            => Regex::VALID_CART_ID,
                'session_id'         => Regex::VALID_SESSION_ID,
                'access_token'       => Regex::VALID_ACCESS_TOKEN,
            )
        );

I want to implement the same functionality as an middleware and make it available for all the routes in Slim 4, but I couldn't find any relevant method in the documentation.

1

There are 1 answers

4
odan On

Slim 4 supports Regular expression matching for the route placeholders.

Example:

$app->get('/users/{id:[0-9]+}', function ($request, $response, array $args) {
    // Find user identified by $args['id']
    // ...
    
    return $response;
});