Add some controllers and actions for user registration.

This commit is contained in:
Dave Smith-Hayes 2024-05-15 21:42:24 -04:00
parent 4832685287
commit e038467a4b
4 changed files with 109 additions and 0 deletions

View File

@ -0,0 +1,57 @@
<?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);
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace Slovocast\Controller\User;
use Psr\Http\Message\ResponseInterface as Response;
use Slovocast\Controller\Controller;
class RegisterUserAction extends Controller
{
public function respond(): Response
{
return $this->respond('user/register/success');
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace Slovocast\Controller\User;
use Psr\Http\Message\ResponseInterface as Response;
use Slovocast\Controller\Controller;
class RegisterUserPage extends Controller
{
public function respond(): Response
{
return $this->render('user/register');
}
}

24
code/src/Routes.php Normal file
View File

@ -0,0 +1,24 @@
<?php
namespace Slovocast;
use Slim\App;
use Slovocast\Controller\User\{
RegisterUserPage,
RegisterUserAction
};
class Routes
{
public static function init(App $app): void
{
// User Routes
self::users($app);
}
protected static function users(App $app): void
{
$app->get('/users/register', RegisterUserPage::class);
$app->post('/users/register', RegisterUserAction::class);
}
}