@@ -170,29 +170,47 @@ public function __construct(RouteCollectionInterface $routes, ?Request $request
170170 $ this ->collection ->setHTTPVerb ($ request ->getMethod () === '' ? $ _SERVER ['REQUEST_METHOD ' ] : $ request ->getMethod ());
171171
172172 $ this ->translateURIDashes = $ this ->collection ->shouldTranslateURIDashes ();
173+ }
173174
174- if ($ this ->collection ->shouldAutoRoute ()) {
175+ /**
176+ * Gets the AutoRouter instance
177+ */
178+ private function getAutoRouter (): AutoRouterInterface
179+ {
180+ if ($ this ->autoRouter === null ) {
175181 $ autoRoutesImproved = config (Feature::class)->autoRoutesImproved ?? false ;
176182 if ($ autoRoutesImproved ) {
177183 assert ($ this ->collection instanceof RouteCollection);
178184
185+ // Only get protected controllers if we're using defined routes
186+ $ protectedControllers = $ this ->collection ->shouldUseDefinedRoutes ()
187+ ? $ this ->collection ->getRegisteredControllers ('* ' )
188+ : [];
189+
179190 $ this ->autoRouter = new AutoRouterImproved (
180- $ this -> collection -> getRegisteredControllers ( ' * ' ) ,
191+ $ protectedControllers ,
181192 $ this ->collection ->getDefaultNamespace (),
182193 $ this ->collection ->getDefaultController (),
183194 $ this ->collection ->getDefaultMethod (),
184195 $ this ->translateURIDashes ,
185196 );
186197 } else {
198+ // Only get CLI routes if we're using defined routes
199+ $ cliRoutes = $ this ->collection ->shouldUseDefinedRoutes ()
200+ ? $ this ->collection ->getRoutes ('CLI ' , false )
201+ : [];
202+
187203 $ this ->autoRouter = new AutoRouter (
188- $ this -> collection -> getRoutes ( ' CLI ' , false ) ,
204+ $ cliRoutes ,
189205 $ this ->collection ->getDefaultNamespace (),
190206 $ this ->collection ->getDefaultController (),
191207 $ this ->collection ->getDefaultMethod (),
192208 $ this ->translateURIDashes ,
193209 );
194210 }
195211 }
212+
213+ return $ this ->autoRouter ;
196214 }
197215
198216 /**
@@ -221,6 +239,37 @@ public function handle(?string $uri = null)
221239 // Restart filterInfo
222240 $ this ->filtersInfo = [];
223241
242+ $ useDefinedRoutes = $ this ->collection ->shouldUseDefinedRoutes ();
243+ $ useAutoRoute = $ this ->collection ->shouldAutoRoute ();
244+
245+ // Let devs know if both are disabled
246+ if (! $ useDefinedRoutes && ! $ useAutoRoute ) {
247+ throw RouterException::forNoRoutingAvailable ();
248+ }
249+
250+ // Fast path 1: Auto-routing ONLY (no defined routes to check)
251+ if ($ useAutoRoute && ! $ useDefinedRoutes ) {
252+ $ this ->autoRoute ($ uri );
253+
254+ return $ this ->controllerName ();
255+ }
256+
257+ // Fast path 2: Defined routes ONLY (no auto-routing fallback)
258+ if ($ useDefinedRoutes && ! $ useAutoRoute ) {
259+ if ($ this ->checkRoutes ($ uri )) {
260+ if ($ this ->collection ->isFiltered ($ this ->matchedRoute [0 ])) {
261+ $ this ->filtersInfo = $ this ->collection ->getFiltersForRoute ($ this ->matchedRoute [0 ]);
262+ }
263+
264+ return $ this ->controller ;
265+ }
266+
267+ throw new PageNotFoundException (
268+ "Can't find a route for ' {$ this ->collection ->getHTTPVerb ()}: {$ uri }'. " ,
269+ );
270+ }
271+
272+ // Original path: BOTH enabled (check defined routes first, then auto-route)
224273 // Checks defined routes
225274 if ($ this ->checkRoutes ($ uri )) {
226275 if ($ this ->collection ->isFiltered ($ this ->matchedRoute [0 ])) {
@@ -388,8 +437,11 @@ public function setIndexPage($page): self
388437 */
389438 public function setTranslateURIDashes (bool $ val = false ): self
390439 {
391- if ($ this ->autoRouter instanceof AutoRouter) {
392- $ this ->autoRouter ->setTranslateURIDashes ($ val );
440+ // Need to get or create the AutoRouter instance
441+ $ autoRouter = $ this ->collection ->shouldAutoRoute () ? $ this ->getAutoRouter () : null ;
442+
443+ if ($ autoRouter instanceof AutoRouter) {
444+ $ autoRouter ->setTranslateURIDashes ($ val );
393445
394446 return $ this ;
395447 }
@@ -591,7 +643,7 @@ static function ($match) use ($matches) {
591643 public function autoRoute (string $ uri )
592644 {
593645 [$ this ->directory , $ this ->controller , $ this ->method , $ this ->params ]
594- = $ this ->autoRouter ->getRoute ($ uri , $ this ->collection ->getHTTPVerb ());
646+ = $ this ->getAutoRouter () ->getRoute ($ uri , $ this ->collection ->getHTTPVerb ());
595647 }
596648
597649 /**
@@ -673,8 +725,11 @@ public function setDirectory(?string $dir = null, bool $append = false, bool $va
673725 $ this ->directory = null ;
674726 }
675727
676- if ($ this ->autoRouter instanceof AutoRouter) {
677- $ this ->autoRouter ->setDirectory ($ dir , $ append , $ validate );
728+ // Need to get or create the AutoRouter instance
729+ $ autoRouter = $ this ->collection ->shouldAutoRoute () ? $ this ->getAutoRouter () : null ;
730+
731+ if ($ autoRouter instanceof AutoRouter) {
732+ $ autoRouter ->setDirectory ($ dir , $ append , $ validate );
678733 }
679734 }
680735
0 commit comments