feat(http): add base controller, home and locale controllers
This commit is contained in:
1 parent
eb7b910daa
commit
a595fd88ff
3 files changed
+67
No files matched your search
@@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Bazaar\Controllers;
|
||||||
|
|
||||||
|
use Bazaar\Core\Context;
|
||||||
|
use Bazaar\Core\Response;
|
||||||
|
|
||||||
|
abstract class Controller
|
||||||
|
{
|
||||||
|
public function __construct(protected readonly Context $ctx)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function view(string $template, array $data = [], string $layout = 'layout'): Response
|
||||||
|
{
|
||||||
|
return $this->ctx->view->render($template, $data, $layout);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function redirect(string $location): Response
|
||||||
|
{
|
||||||
|
return Response::redirect($location);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function back(string $fallback = '/'): Response
|
||||||
|
{
|
||||||
|
$referer = $this->ctx->request->header('Referer');
|
||||||
|
return Response::redirect($referer ?? $fallback);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function notFound(): Response
|
||||||
|
{
|
||||||
|
return $this->ctx->view->render('errors.404', [], 'layout', 404);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Bazaar\Controllers;
|
||||||
|
|
||||||
|
use Bazaar\Core\Response;
|
||||||
|
|
||||||
|
final class HomeController extends Controller
|
||||||
|
{
|
||||||
|
public function index(): Response
|
||||||
|
{
|
||||||
|
return $this->view('home', [
|
||||||
|
'featured' => $this->ctx->products->featured(6),
|
||||||
|
'categories' => $this->ctx->categories->withProductCounts(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Bazaar\Controllers;
|
||||||
|
|
||||||
|
use Bazaar\Core\Response;
|
||||||
|
use Bazaar\Support\Translator;
|
||||||
|
|
||||||
|
final class LocaleController extends Controller
|
||||||
|
{
|
||||||
|
public function set(string $locale): Response
|
||||||
|
{
|
||||||
|
if (Translator::isSupported($locale)) {
|
||||||
|
$this->ctx->session->put('locale', $locale);
|
||||||
|
}
|
||||||
|
return $this->back('/');
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user