Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Routes

A route is a rule: "for this HTTP method and this address, run this code". While the project is a single file, routes are declared in public/index.php between creating the application and the $app->run(); line.

A simple route

// public/index.php
$app->get('/posts', function (Request $request, Response $response) {
    $response->getBody()->write('List of posts');
 
    return $response;
});

The address http://localhost:8080/posts shows "List of posts". The function that builds the response is called the route handler.

HTTP methods

The browser sends requests with different methods: GET opens a page, POST submits a form. Each method has its own application method:

CallTypical use
$app->get()Show a page or data
$app->post()Create a record, accept a form
$app->put(), $app->patch()Change a record
$app->delete()Delete a record
$app->map(['GET', 'POST'], ...)One handler for several methods
$app->any()Any method

One address can answer different methods differently: GET /posts shows the list, POST /posts creates a post.

A placeholder in the address

A part of the address in curly braces is a placeholder. Its value arrives in the handler's third argument, the $args array:

// public/index.php
$app->get('/posts/{id}', function (Request $request, Response $response, array $args) {
    $response->getBody()->write('Post #' . (int) $args['id']);
 
    return $response;
});

The address /posts/5 prints "Post #5". A placeholder value is always a string, so a number is cast with (int).

Constraining a placeholder

After a colon comes a regular expression — a pattern the value must match:

// public/index.php
$app->get('/posts/{id:[0-9]+}', function (Request $request, Response $response, array $args) {
    $response->getBody()->write('Post #' . $args['id']);
 
    return $response;
});

[0-9]+ means "one or more digits". The address /posts/5 matches, while /posts/abc gets a 404 error from Slim.

An optional part

A part of the address in square brackets may be omitted:

// public/index.php
$app->get('/news[/{page:[0-9]+}]', function (Request $request, Response $response, array $args) {
    $page = (int) ($args['page'] ?? 1);
 
    $response->getBody()->write("News, page $page");
 
    return $response;
});

The route matches both /news and /news/3. When the part is missing, $args has no page key either — which is why the default is set with ??.

Trying other methods

From the address bar a browser sends only GET. Other methods are easy to try from the terminal with curl:

curl -X POST http://localhost:8080/posts

If the address has a route but for a different method, Slim answers with a 405 "Method Not Allowed" error.

Common mistake

Two routes with the same method and the same pattern cannot be declared: /posts/{id} and /posts/{slug} are one and the same address to Slim, and the application fails with an error. Separate them with constraints — {id:[0-9]+} and {slug:[a-z-]+} — or with different prefixes.

Thanks for reading!

Donate to the site — we work hard for you.