💡 Add function to redefine default parameter constraint

This commit is contained in:
Skylear 2021-07-15 16:17:53 -05:00
parent 3a1e5b65d7
commit 8a523c3ed2

View File

@ -12,9 +12,11 @@ class Router
/** /**
* 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.
*
* @param string $route The path to be used as the route. * @param string $route The path to be used as the route.
* @param callable|string $action Either a callable to be executed, or a string reference to a method. * @param callable|string $action Either a callable to be executed, or a string reference to a method.
* @param string|array $methods The HTTP verb(s) this route accepts. * @param string|array $methods The HTTP verb(s) this route accepts.
* @return 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')
{ {
@ -52,6 +54,17 @@ class Router
self::$methodNotAllowed = $action; self::$methodNotAllowed = $action;
} }
/**
* Redefine the default constraint for route parameters. Default is '([\w\-]+)'
*
* @param string $constraint The RegEx you want parameters to adhere to by default. Defaults to '([\w\-]+)'
* @return void
*/
public static function setDefaultConstraint(string $constraint = '([\w\-]+)')
{
self::$defaultConstraint = $constraint;
}
public static function with(string|array $parameter, string $constraint = '') public static function with(string|array $parameter, string $constraint = '')
{ {
if (is_array($parameter)) { if (is_array($parameter)) {