Add some controllers and actions for user registration.
This commit is contained in:
parent
4832685287
commit
e038467a4b
57
code/src/Controller/Controller.php
Normal file
57
code/src/Controller/Controller.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
14
code/src/Controller/User/RegisterUserAction.php
Normal file
14
code/src/Controller/User/RegisterUserAction.php
Normal 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');
|
||||||
|
}
|
||||||
|
}
|
14
code/src/Controller/User/RegisterUserPage.php
Normal file
14
code/src/Controller/User/RegisterUserPage.php
Normal 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
24
code/src/Routes.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user