Routing Issue "Page Not Found"

I’m learning on the fly here, Php and the Slim Framework. I’m having an issue with the routing.

This works just fine:

11 // add new Route 
12 $app->get("/", function () {
13     echo "<h1>Hello Slim World</h1>";
14 });
15 
16 $app->get('/hello/{name}', function (Request $request, Response $response) {
17     $name = $request->getAttribute('name');
18     $response->getBody()->write("Hello, $name");
19 
20     return $response;
21 });

But this is not working, as in it returns a Page Not Found error:

     // GET http://myserver.net/api/test?id=1
23 // Get a single user by id
24 $app->get('/test/:id', function (Request $request, Response $response) {
25     echo "HELLOOWWWWWWWW";
26 });

Any suggestions what I may be doing wrong? I’m using PHP 5.6 and Apache 2.4.10 on Raspbian.

In your GET request you have a path that includes /api but that isn’t represented in your route definition, so that is where it likely isn’t matching based on what you’ve shown.

I have altered the route to include the working directory just to test it out, but still keep getting the Page Not Found error. Also, the routes that are in this format:

// GET http://myserver.net/api/hello/bob
$app->get('/hello/{name}', function (Request $request, Response $response) {

…work just fine as they are, but if I modify them as you suggested they no longer work.

Ah, okay so your index.php is within /api, that is helpful to know. :slight_smile:

Your route includes a slash after test but your get request doesn’t have that. Also, you’d typically get your query parameters from the request object.

/// GET http://myserver.net/api/test?id=1 where index.php is within api/
$app->get('/test', function ($request, $response) {
    $params = $request->getQueryParams();
    return $response->write("Hello " . var_dump($params));
});

This method did work for my routing function. Any idea why the :id part doesn’t work as in the tutorial? I tried eliminating the slash but still no success. So so far, eliminating the query params from the routing definition gets me back on track, it just doesn’t track with all the tutorials.

Thanks for your help!

I’m not sure if you might be confusing route placeholders with query parameters. Or in other words you might be confusing the path versus the query string. If we look at a URL like this:

http://example.com/2016/12/20/news?sort=desc

Then in this case 2016, 12, 20, and tags are (probably) route placeholders (path) you will need to match in your route definition while sort is a query parameter (from the query string) with a value of desc.

$app->get('/{year}/{month}/{day}/{tag}', function ($request, $response, $args) {
    // responds to URLs like http://example.com/2016/12/20/tags?sort=desc
    // in reality you'd likely want to use regular expression matching for numeric and length:
    // https://www.slimframework.com/docs/objects/router.html#regular-expression-matching

    $year = $args['year']; // = 2016
    $month = $args['month']; // = 12
    $day = $args['day']; // = 20
    $tag = $args['tag']; // = news

    $sort = $request->getQueryParam('sort', $default = 'asc'); // = desc if provided, asc otherwise

    return $response;
});

In the tutorial, the id is a route placeholder not a query parameter.

1 Like