diff --git a/src/Domain/Controller.php b/src/Domain/Controller.php
index 1b1a4e0..4311c0b 100644
--- a/src/Domain/Controller.php
+++ b/src/Domain/Controller.php
@@ -19,6 +19,7 @@ abstract class Controller {
$defaults = [
'routeController' => static::class,
'routeMethods' => [ 'GET' ],
+ 'pageHeadTitle' => 'Page',
];
$args = array_merge($defaults, $args);
diff --git a/src/Domain/Pages/PagesController.php b/src/Domain/Pages/PagesController.php
index 33e47db..3d8612b 100644
--- a/src/Domain/Pages/PagesController.php
+++ b/src/Domain/Pages/PagesController.php
@@ -14,8 +14,8 @@ class PagesController extends Controller {
public static function defineRoutes(): array
{
return [
- self::Route( routeUrl: '/', routeName: 'Homepage', routeAction: 'index' ),
- self::Route( routeUrl: '/test/{string}/baba', routeName: 'test', routeAction: 'test' ),
+ self::Route( routeUrl: '/', routeName: 'home', routeAction: 'index', pageHeadTitle: "Home Page" ),
+ self::Route( routeUrl: '/test/{int}/', routeName: 'test', routeAction: 'test' ),
];
}
diff --git a/src/Http/Route.php b/src/Http/Route.php
index cc3d9e0..e4872a5 100644
--- a/src/Http/Route.php
+++ b/src/Http/Route.php
@@ -13,19 +13,30 @@ final class Route {
public private(set) string $routeController;
public private(set) string $routeAction;
public private(set) array $routeMethods;
+ public private(set) string $pageHeadTitle;
+ /**
+ * @param string $routeUrl Le chemin vers la route.
+ * @param string $routeName Le nom de la route.
+ * @param string $routeController Le controller qui va interpréter cette route.
+ * @param string $routeAction L'action du controller qui va interpréter cette route.
+ * @param array $routeMethods Une liste de methodes GET, POST que la page accepte.
+ * @param string $pageHeadTitle Le nom de la page qui sera affiché dans
.
+ */
public function __construct(
string $routeUrl,
string $routeName,
string $routeController,
string $routeAction,
- array $routeMethods
+ array $routeMethods,
+ string $pageHeadTitle,
){
$this->routeUrl = $routeUrl;
$this->routeName = $routeName;
$this->routeController = $routeController;
$this->routeAction = $routeAction;
$this->routeMethods = $routeMethods;
+ $this->pageHeadTitle = $pageHeadTitle;
}
}
\ No newline at end of file
diff --git a/src/Http/Router.php b/src/Http/Router.php
index 3c04833..5887b75 100644
--- a/src/Http/Router.php
+++ b/src/Http/Router.php
@@ -129,10 +129,12 @@ final class Router {
private static function clientRouteExist(): Route|bool {
$clientRouteName = trim( self::$clientRouteString, '/' );
+
foreach( self::$routes as $route ){
+
$routeName = self::getRegexRoute( $route );
if( preg_match( $routeName, $clientRouteName, $matches ) ){
- array_shift( $matches );
+ array_shift( $matches ); // On enlève la chaine complète.
self::$clientRouteArguments = $matches;
return $route;
}
@@ -142,6 +144,12 @@ final class Router {
}
+ /**
+ * Permet de remplacer les expressions définies dans la configuration par leurs équivalents Regex.
+ *
+ * @param Route $route
+ * @return string
+ */
private static function getRegexRoute( Route $route ): string {
$routeUrl = trim( $route->routeUrl, '/' );
foreach ( Kernel::$configs['route_arguments'] as $key => $value ){
@@ -161,4 +169,27 @@ final class Router {
new $controller()->$method( ...self::$clientRouteArguments);
}
+ /**
+ * Permet d'obtenir l'adresse du site vers une route spécifique.
+ *
+ * @param string $routeName Le nom de la route.
+ * @param ...$args - Les arguments de la route, si existants.
+ *
+ * @return string Renvoie l'URL demandé ou bien la racine du site si la route n'existe pas.
+ */
+ public static function getRouteURL( string $routeName, ...$args ): string {
+ foreach( self::$routes as $route ){
+ if( $routeName === $route->routeName ){
+ $i = 0;
+ $routeUrl = preg_replace_callback( '/\{([^}]+)}/', function( $match ) use ( $args, &$i ){
+ return $args[$i++] ?? "";
+ }, $route->routeUrl);
+
+ return rtrim( Kernel::$configs['general']['website_url'] . $routeUrl, '/' );
+ }
+ }
+
+ return Kernel::$configs['general']['website_url'];
+ }
+
}
\ No newline at end of file
diff --git a/src/Infrastructure/View.php b/src/Infrastructure/View.php
index 2edf666..4420fe5 100644
--- a/src/Infrastructure/View.php
+++ b/src/Infrastructure/View.php
@@ -132,10 +132,10 @@ final class View {
} else {
// Si on a un squelette, on charge tout le contenu de la vue enfante.
- $content = file_get_contents( self::VIEW_PATH . $this->viewName );
+ $contentName = $this->viewName;
// On démarre la vue du squelette.
- $base = new View( $this->skeleton, $this->viewArgs, skeleton: null, integratedContent: [ 'content' => $content ] );
+ $base = new View( $this->skeleton, $this->viewArgs, skeleton: null, integratedContent: [ 'content' => $contentName ] );
}
@@ -154,7 +154,7 @@ final class View {
*/
public static function inject( string $integratedContentName ): void {
if( isset( self::$lastInstance->integratedContent[ $integratedContentName ] ) )
- echo self::$lastInstance->integratedContent[ $integratedContentName ];
+ require self::VIEW_PATH . self::$lastInstance->integratedContent[ $integratedContentName ];
}
/**
@@ -186,9 +186,25 @@ final class View {
}
}
+ /**
+ * Permet d'obtenir le titre de la page à mettre dans
+ * @return string
+ */
public static function getHeadTitle(): string {
$siteUrl = Kernel::$configs['general']['website_name'];
- return Router::$clientRoute->routeName . ' - ' . $siteUrl;
+ return Router::$clientRoute->pageHeadTitle . ' - ' . $siteUrl;
+ }
+
+ /**
+ * Permet d'obtenir une route
+ *
+ * @param string $routeName Le nom de la route.
+ * @param ...$args - Les arguments de la route, si existant.
+ *
+ * @return void Affiche uniquement.
+ */
+ public static function routeUrl( string $routeName, ...$args ): void {
+ echo Router::getRouteURL( $routeName, ...$args );
}
}
\ No newline at end of file
diff --git a/views/home.php b/views/home.php
index eda17ba..3cb93ff 100644
--- a/views/home.php
+++ b/views/home.php
@@ -1 +1,2 @@
-Coucou
\ No newline at end of file
+Coucou
+
\ No newline at end of file