Getting parameter action function Slim 4

This would work if you use the __invoke method instead of the action() method as name.

Example:

<?php

namespace App\Action\Home;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

final class HomeAction
{
    public function __invoke(
        ServerRequestInterface $request,
        ResponseInterface $response,
        array $args
    ): ResponseInterface {
        $id = $args['id'];
        
        $response->getBody()->write(sprintf('The ID is: %s', $id));

        return $response;
    }
}
1 Like