Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Route parameters with Slim 3 & built-in PHP server

Tags:

php

slim

I'm having problems getting routes with parameters to work in Slim 3 RC.

$app->get('/hello/:name', function($req, $res, $args) {
    echo "Hello {$name}";
});

Visiting /hello/joe results in 404.

Other routes work fine, e.g.:

$app->get('/', HomeAction::class . ":dispatch");

$app->get('/services', ServicesAction::class . ":dispatch");

I am using the built-in PHP server while I am developing. I do not have any .htaccess file. I have tried the suggested route.php suggestion and the accepted answer from this question but it does not work. Any suggestions please?

like image 558
gazareth Avatar asked Oct 04 '15 18:10

gazareth


1 Answers

From Slim 3 you need to change :name in {name}.

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

You can find the documentation here.

like image 159
Federkun Avatar answered Oct 20 '22 18:10

Federkun