Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENH] get formatted route path via function #176

Merged
merged 3 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions config/function.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,13 @@ function tra(string $message, $value = null): string
$i18n = new i18n(Session::get('lang'));
$translate_value = !is_array($value) ? [$value] : $value;
return $i18n->translate($message, $translate_value);
}

/**
* get a formatted application route path
* @param string $path
* @return string
*/
function route_path(string $path): string{
bim-g marked this conversation as resolved.
Show resolved Hide resolved
return WEB_ROOT . ltrim($path,'/');
}
3 changes: 1 addition & 2 deletions config/globals.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@
'lang' => 'fr',
'vendor' => true,
'autoload' => ['src', 'controller', 'middleware', 'models', 'helpers'],
'helper' => WEB_ROOT,
'preferences' => WEB_ROOT . 'helper'
'helpers' => WEB_ROOT,
];
2 changes: 1 addition & 1 deletion src/Core/MetaData.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ protected function getLink(bool $twitter = false): string
/**
* @return string|null
*/
protected function getType(bool $twitter = fals): string
protected function getType(bool $twitter = false): string
{
$type = $twitter ? "<meta property=\"og:type\" content=\"$this->_type\" />" : "<meta name=\"twitter:type\" content=\"article\" />";
return $this->_type ? $type : '';
Expand Down
13 changes: 7 additions & 6 deletions src/Core/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function setFolder(string $folder_name)
*
* @param string $view
*/
function display(string $view)
public function display(string $view)
{
$view_file = $this->buildFilePath($view);
$render = $this->renderView($view_file);
Expand Down Expand Up @@ -154,8 +154,7 @@ protected function renderView(string $view)
*/
private function renderNotDefined(string $file_name)
{
Response::setStatusCode(404);
print_r(['exception' => "$file_name file path is not correct."]);
Response::send(['exception' => "$file_name file path is not correct."],404);
}

/**
Expand Down Expand Up @@ -208,12 +207,12 @@ private function buildAssetHead($html)
$xpath = new DOMXPath($dom);
$head = $xpath->query('//head/title');
$template = $dom->createDocumentFragment();
// add style link to the head tag of the page
// add a style link to the head tag of the page
foreach (self::$stylelink as $k => $v) {
$template->appendXML('<link rel="stylesheet" type="text/css" href="' . $v . '">');
$head[0]->parentNode->insertbefore($template, $head[0]->nextSibling);
}
// add script link to the head of the page
// add a script link to the head of the page
foreach (self::$jslink as $k => $v) {
$link = $v['link'];
$src = '<script src="' . $link . '" type="text/javascript"></script>';
Expand All @@ -224,7 +223,9 @@ private function buildAssetHead($html)
// add metadata to the head of the page
if (self::$metadata) {
$template->appendXML(self::$metadata);
$head[0]->parentNode->insertbefore($template, $head[0]->nextSibling);
if ($head[0]) {
$head[0]->parentNode->insertbefore($template, $head[0]->nextSibling);
}
}
print(html_entity_decode($dom->saveHTML()));
} catch (Exception $ex) {
Expand Down
2 changes: 1 addition & 1 deletion views/404.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<body>
<div class="w3-margin w3-center">
<h1 class="w3-text-green ">404</h1>
<p><a href="<?= WEB_ROOT ?>">Go home</a></p>
<p><a href="<?= route_path('/') ?>">Go home</a></p>
</div>
</body>
</html>
2 changes: 1 addition & 1 deletion views/home.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
<?php } ?>
<div class="w3-card w3-border w3-round-large " style="width: 300px;overflow: hidden">
<h3 class="w3-text-blue-gray w3-padding"><?= $language->translate("Change the language") ?></h3>
<form action="<?= WEB_ROOT . "changelang" ?>" method="post">
<form action="<?= route_path("/changelang") ?>" method="post">
<input type="hidden" name="token" value="<?= Token::generate() ?>">
<select name="lang" id="lang_id" class="w3-select w3-center">
<option value="" class="w3-center w3-large" disabled selected><?= $lang ?></option>
Expand Down
Loading