slovocast/app/src/Controller/Controller.php

58 lines
1.5 KiB
PHP
Raw Normal View History

<?php
namespace Slovocast\Controller;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Views\Twig;
use Slim\Routing\RouteContext;
abstract class Controller
{
protected Request $request;
protected Response $response;
protected array $args;
protected RouteContext $routeContext;
/**
* Make the Class invokable and pass it into a route as its handler.
*
* @param Request $request
* @param Response $response
* @param array $args List of possible URL arguments
* @return Response
*/
public function __invoke(
Request $request,
Response $response,
array $args = []
): Response {
$this->request = $request;
$this->response = $response;
$this->args = $args;
$this->routeContext = RouteContext::fromRequest($this->request);
return $this->respond($request, $response);
}
/**
* Implement this method for handling the request.
*
* @return Response
*/
abstract public function respond(): Response;
/**
* Render the given Template.
*
* @param string $templateName The name of the template
* @param array $data The data for the template
* @return Response
*/
public function render(string $templateName, array $data = []): Response
{
$view = Twig::fromRequest($this->request);
return $view->render($this->response, $templateName, $data);
}
}