Pragmatism in the real world

Default route arguments in Slim

A friend of mine recently asked how to do default route arguments and route specific configuration in Slim, so I thought I’d write up how to do it.

Consider a simple Hello route:

$app->get("/hello[/{name}]", function ($request, $response, $args) {
    $name = $request->getAttribute('name');
    return $response->write("Hello $name");
})

This will display “Hello ” for the URL /hello and “Hello Rob” for the URL /hello/Rob.

If we wanted a default of “World”, we can set an argument on the Route object that is returned from get() (and all the other routing methods):

$app->get("/hello[/{name}]", function ($request, $response, $args) {
    $name = $request->getAttribute('name');
    return $response->write("Hello $name");
})->setArgument('name', 'World');

This works exactly as you would expect.

The route arguments don’t have to be placeholder and you can set multiple route arguments. For example:

$app->get("/hello[/{name}]", function ($request, $response, $args) {
    $name = $request->getAttribute('name');
    $foo = $request->getAttribute('foo');
    return $response->write("Hello $name, foo = $foo");
})->setArguments([
	'name' => 'World,
	'foo' => 'bar',
]);

Now, we have a foo attribute in our request, which is a per-route configuration option that you can do with as you wish – e.g. setting acl rules like this:

$app->get("/users", App\HelloAction::class)->setArgument("role", "userAdministrator");

4 thoughts on “Default route arguments in Slim

  1. Hi ROB @akrabat !!!
    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**

Comments are closed.