-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseViewController.php
More file actions
50 lines (44 loc) · 1.51 KB
/
Copy pathBaseViewController.php
File metadata and controls
50 lines (44 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
// src/Libs/Italix/Mvc/BaseViewController.php
declare(strict_types=1);
namespace Italix\Mvc;
use Psr\Http\Message\ResponseInterface as Response;
/**
* Convenience base for controllers that render HTML views.
*
* Provides:
* - $view (ViewRenderer) injected automatically
* - show(string $path, array $data): Response — render + wrap in 200 HTML response
*
* If your controller needs additional services, merge with the parent in depends_on():
*
* public static function depends_on(string $method = ''): array
* {
* return array_merge(parent::depends_on($method), [
* 'dm' => DataManager::class,
* ]);
* }
*
* Forgetting the merge means $view will not be injected and the first call to
* $this->view will throw an "Uninitialized property" error.
*/
abstract class BaseViewController extends BaseController
{
protected ViewRenderer $view;
public static function depends_on(string $method = ''): array
{
return ['view' => ViewRenderer::class];
}
protected function show(string $path, array $data = []): Response
{
$html = $this->view->render($path, $data);
$response = Responses::make(200);
$response->getBody()->write($html);
return $response->withHeader('Content-Type', 'text/html; charset=utf-8');
}
}