Fix view skeleton include bug and add RouteURL method.
This commit is contained in:
@@ -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 <title></title>.
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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'];
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user