Initilazie 2.0

This commit is contained in:
Viktor Sergeev
2025-06-12 16:04:19 +02:00
parent a2a29195d7
commit 9eaf948a9f
24 changed files with 1034 additions and 0 deletions

25
Library/ErrorMsg.php Normal file
View File

@@ -0,0 +1,25 @@
<?php
namespace Blog\Library;
use Blog\Library\View;
class ErrorMsg
{
protected $view;
public function __construct($msg = 'Ihre Anfrage konnte nicht verarbeitet werden', $ex = '')
{
$this->view = new View(dirname(__DIR__).DIRECTORY_SEPARATOR.'Views'
, 'Error', 'showErrMsg');
$this->view->setVars([
'error' => $msg,
'debug' => $ex
]);
$this->view->render();
}
}

58
Library/View.php Normal file
View File

@@ -0,0 +1,58 @@
<?php
namespace Blog\Library;
class View
{
protected $path, $controller, $do, $vars = [];
/**
* @param string $path Basepath of the views.
* @param string $controllerName Current controller.
* @param string $doMethodName Current action.
*/
public function __construct($path, $controllerName, $doMethodName)
{
$this->path = $path;
$this->controller = $controllerName;
$this->do = $doMethodName;
}
/**
* Set view vars. The keys will be added, to existing keys.
*
* @param array $vars
*/
public function setVars(array $vars)
{
foreach ($vars as $key => $val) {
$this->vars[$key] = $val;
}
}
public function setDoMethodName($doMethodName)
{
$this->do = $doMethodName;
}
/**
* Render the view.
*
* @throws NotFoundException
*/
public function render()
{
$fileName = $this->path.DIRECTORY_SEPARATOR.$this->controller.DIRECTORY_SEPARATOR.$this->do.'.phtml';
if (!file_exists($fileName)) {
echo "Fehler: Datei " . $fileName . " existiert nicht";
exit();
}
// spare the view the bloat of using "$this->vars[]" for every variable
foreach ($this->vars as $key => $val) {
$$key = $val; //Variable Variablen siehe http://php.net/manual/de/language.variables.variable.php
}
include $fileName;
}
}