Slim: Default route in groups?

Created on 24 Nov 2015  路  3Comments  路  Source: slimphp/Slim

So as it stands right now I can have:

<?php
$app->get('/user', function($request, $response, $args) {
    /* code */
}):
?>

Which can then correctly resolve to domain.com/user. My problem is when I want to logically group the routes. If I define it like this:

<?php
$app->group('/user', function() use ($app) {
    $app->get('/', function($request, $response, $args) {
        /* code */
    });
});
?>

Then it forces a match to domain.com/user/ (note the trailing slash). Is there a way to set up a group route with a "default" so it doesn't require that trailing slash?

Most helpful comment

Note that with #1626, the URI path is very much what-you-see-is-what-you-get. In this case, nothing changes and so to route to /user when your group is /user, you need an empty path in your map:

$app->group('/user', function() use ($app) {
    $app->get('', function($request, $response, $args) {
        // URL to get here is: /user
    });
});

All 3 comments

See #1597

Given this set up:

$ mkdir slimtest && cd slimtest/
$ composer require slim/slim ^3.0@RC
$ touch index.php

with this index.php:

<?php
require 'vendor/autoload.php';
$settings = [
    'settings' => [
        'displayErrorDetails' => true,
    ],
];
$app = new \Slim\App($settings);

$app->group('/user', function () use ($app) {
    $app->get('/', function ($request, $response, $args) {
        return $response->write("/user/\n");
    });
    $app->get('', function ($request, $response, $args) {
        return $response->write("/user\n");
    });
});
$app->run();

Then running: php -S 0.0.0.0:8888, I get this when I curl:

$ curl http://localhost:8888/user
/user
$ curl http://localhost:8888/user/
/user/

i.e. it works as you would expect.

Note that with #1626, the URI path is very much what-you-see-is-what-you-get. In this case, nothing changes and so to route to /user when your group is /user, you need an empty path in your map:

$app->group('/user', function() use ($app) {
    $app->get('', function($request, $response, $args) {
        // URL to get here is: /user
    });
});
Was this page helpful?
0 / 5 - 0 ratings

Related issues

akrabat picture akrabat  路  43Comments

zyx-rd picture zyx-rd  路  19Comments

Bilge picture Bilge  路  28Comments

grikdotnet picture grikdotnet  路  54Comments

l0gicgate picture l0gicgate  路  31Comments