use Slim\Routing\RouteContext;
// ...
$routeArguments = RouteContext::fromRequest($request)
->getRoute()
->getArguments();
foreach ($routeArguments as $key => $argument) {
if (!is_null($model = $routeToModel[$key])) {
$request = $request->withAttribute($key, $model::find($argument));
}
}
and in controller
use App\Models\User;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
public function show(Request $request, Response $response)
{
return $this->view->render($response, 'auth/user.twig', [
'user' => $request->getAttribute('user')
]);
}