Search code examples
routesslimslim-3

Slim 3 - how add or-option into the route?


How can I have or-option in the Slim 3 routes?

For instance, this is what I do currently:

// Home page.
$app->get('/', function (Request $request, Response $response, array $args) {
    // Get the application settings.
    $settings = $this->get('settings');

    // Check if the home page class is provided.
    ... lots of codes
});

$app->get('/home', function (Request $request, Response $response, array $args) {
    // Get the application settings.
    $settings = $this->get('settings');

    // Check if the home page class is provided.
    ... lots of codes
});

instead of repeating the chunks of these codes, can I make them into one like:

$app->get('/ or /home', function (Request $request, Response $response, array $args) {
...}

Solution

  • Optional segments is what you're looking for.