🚧 Build new prefix group type
This commit is contained in:
parent
9a04c906a7
commit
1393e0face
|
@ -9,6 +9,7 @@ class Router
|
||||||
private static $pathNotFound;
|
private static $pathNotFound;
|
||||||
private static $methodNotAllowed;
|
private static $methodNotAllowed;
|
||||||
private static string $defaultConstraint = '([\w\-]+)';
|
private static string $defaultConstraint = '([\w\-]+)';
|
||||||
|
private static string $currentPrefix = '';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A quick static function to register a route in the router. Used by the shorthand methods as well.
|
* A quick static function to register a route in the router. Used by the shorthand methods as well.
|
||||||
|
@ -20,6 +21,11 @@ class Router
|
||||||
*/
|
*/
|
||||||
public static function add(string $route, callable|string $action, string|array $methods = 'GET')
|
public static function add(string $route, callable|string $action, string|array $methods = 'GET')
|
||||||
{
|
{
|
||||||
|
// If a prefix exists, prepend it to the route
|
||||||
|
if (!empty(self::$currentPrefix)) {
|
||||||
|
$route = self::$currentPrefix.$route;
|
||||||
|
}
|
||||||
|
|
||||||
self::$routes[] = [
|
self::$routes[] = [
|
||||||
'route' => $route,
|
'route' => $route,
|
||||||
'action' => $action,
|
'action' => $action,
|
||||||
|
@ -96,6 +102,22 @@ class Router
|
||||||
self::$defaultConstraint = $constraint;
|
self::$defaultConstraint = $constraint;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Accepts a callable that defines routes, and adds a prefix to them.
|
||||||
|
*
|
||||||
|
* @param string $prefix The prefix you want added to the routes.
|
||||||
|
* @param callable $routes A function that defines routes.
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public static function prefix(string $prefix, callable $routes)
|
||||||
|
{
|
||||||
|
self::$currentPrefix = $prefix;
|
||||||
|
|
||||||
|
$routes();
|
||||||
|
|
||||||
|
self::$currentPrefix = '';
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Define a constraint for a route parameter. If only passing one parameter,
|
* Define a constraint for a route parameter. If only passing one parameter,
|
||||||
* provide the parameter name as first argument and constraint as second. If
|
* provide the parameter name as first argument and constraint as second. If
|
||||||
|
|
Loading…
Reference in New Issue
Block a user