@@ -12,6 +12,8 @@ class Router
1212 public const PLACEHOLDER_TOKEN = '::: ' ;
1313 public const WILDCARD_TOKEN = '* ' ;
1414
15+ protected static bool $ allowOverride = false ;
16+
1517 /**
1618 * @var array<string,Route[]>
1719 */
@@ -40,6 +42,30 @@ public static function getRoutes(): array
4042 return self ::$ routes ;
4143 }
4244
45+ /**
46+ * Get allow override
47+ *
48+ *
49+ * @return bool
50+ */
51+ public static function getAllowOverride (): bool
52+ {
53+ return self ::$ allowOverride ;
54+ }
55+
56+ /**
57+ * Set Allow override
58+ *
59+ *
60+ * @param bool $value
61+ * @return void
62+ */
63+ public static function setAllowOverride (bool $ value ): void
64+ {
65+ self ::$ allowOverride = $ value ;
66+ }
67+
68+
4369 /**
4470 * Add route to router.
4571 *
@@ -55,7 +81,7 @@ public static function addRoute(Route $route): void
5581 throw new Exception ("Method ( {$ route ->getMethod ()}) not supported. " );
5682 }
5783
58- if (array_key_exists ($ path , self ::$ routes [$ route ->getMethod ()])) {
84+ if (array_key_exists ($ path , self ::$ routes [$ route ->getMethod ()]) && ! self :: $ allowOverride ) {
5985 throw new Exception ("Route for ( {$ route ->getMethod ()}: {$ path }) already registered. " );
6086 }
6187
@@ -77,7 +103,7 @@ public static function addRouteAlias(string $path, Route $route): void
77103 {
78104 [$ alias ] = self ::preparePath ($ path );
79105
80- if (array_key_exists ($ alias , self ::$ routes [$ route ->getMethod ()])) {
106+ if (array_key_exists ($ alias , self ::$ routes [$ route ->getMethod ()]) && ! self :: $ allowOverride ) {
81107 throw new Exception ("Route for ( {$ route ->getMethod ()}: {$ alias }) already registered. " );
82108 }
83109
0 commit comments