@@ -158,29 +158,47 @@ public function __construct(RouteCollectionInterface $routes, ?Request $request
158158 $ this ->collection ->setHTTPVerb ($ request ->getMethod () === '' ? $ _SERVER ['REQUEST_METHOD ' ] : $ request ->getMethod ());
159159
160160 $ this ->translateURIDashes = $ this ->collection ->shouldTranslateURIDashes ();
161+ }
161162
162- if ($ this ->collection ->shouldAutoRoute ()) {
163+ /**
164+ * Gets the AutoRouter instance
165+ */
166+ private function getAutoRouter (): AutoRouterInterface
167+ {
168+ if ($ this ->autoRouter === null ) {
163169 $ autoRoutesImproved = config (Feature::class)->autoRoutesImproved ?? false ;
164170 if ($ autoRoutesImproved ) {
165171 assert ($ this ->collection instanceof RouteCollection);
166172
173+ // Only get protected controllers if we're using defined routes
174+ $ protectedControllers = $ this ->collection ->shouldUseDefinedRoutes ()
175+ ? $ this ->collection ->getRegisteredControllers ('* ' )
176+ : [];
177+
167178 $ this ->autoRouter = new AutoRouterImproved (
168- $ this -> collection -> getRegisteredControllers ( ' * ' ) ,
179+ $ protectedControllers ,
169180 $ this ->collection ->getDefaultNamespace (),
170181 $ this ->collection ->getDefaultController (),
171182 $ this ->collection ->getDefaultMethod (),
172183 $ this ->translateURIDashes ,
173184 );
174185 } else {
186+ // Only get CLI routes if we're using defined routes
187+ $ cliRoutes = $ this ->collection ->shouldUseDefinedRoutes ()
188+ ? $ this ->collection ->getRoutes ('CLI ' , false )
189+ : [];
190+
175191 $ this ->autoRouter = new AutoRouter (
176- $ this -> collection -> getRoutes ( ' CLI ' , false ) ,
192+ $ cliRoutes ,
177193 $ this ->collection ->getDefaultNamespace (),
178194 $ this ->collection ->getDefaultController (),
179195 $ this ->collection ->getDefaultMethod (),
180196 $ this ->translateURIDashes ,
181197 );
182198 }
183199 }
200+
201+ return $ this ->autoRouter ;
184202 }
185203
186204 /**
@@ -209,6 +227,37 @@ public function handle(?string $uri = null)
209227 // Restart filterInfo
210228 $ this ->filtersInfo = [];
211229
230+ $ useDefinedRoutes = $ this ->collection ->shouldUseDefinedRoutes ();
231+ $ useAutoRoute = $ this ->collection ->shouldAutoRoute ();
232+
233+ // Let devs know if both are disabled
234+ if (! $ useDefinedRoutes && ! $ useAutoRoute ) {
235+ throw RouterException::forNoRoutingAvailable ();
236+ }
237+
238+ // Fast path 1: Auto-routing ONLY (no defined routes to check)
239+ if ($ useAutoRoute && ! $ useDefinedRoutes ) {
240+ $ this ->autoRoute ($ uri );
241+
242+ return $ this ->controllerName ();
243+ }
244+
245+ // Fast path 2: Defined routes ONLY (no auto-routing fallback)
246+ if ($ useDefinedRoutes && ! $ useAutoRoute ) {
247+ if ($ this ->checkRoutes ($ uri )) {
248+ if ($ this ->collection ->isFiltered ($ this ->matchedRoute [0 ])) {
249+ $ this ->filtersInfo = $ this ->collection ->getFiltersForRoute ($ this ->matchedRoute [0 ]);
250+ }
251+
252+ return $ this ->controller ;
253+ }
254+
255+ throw new PageNotFoundException (
256+ "Can't find a route for ' {$ this ->collection ->getHTTPVerb ()}: {$ uri }'. " ,
257+ );
258+ }
259+
260+ // Original path: BOTH enabled (check defined routes first, then auto-route)
212261 // Checks defined routes
213262 if ($ this ->checkRoutes ($ uri )) {
214263 if ($ this ->collection ->isFiltered ($ this ->matchedRoute [0 ])) {
@@ -361,8 +410,11 @@ public function setIndexPage($page): self
361410 */
362411 public function setTranslateURIDashes (bool $ val = false ): self
363412 {
364- if ($ this ->autoRouter instanceof AutoRouter) {
365- $ this ->autoRouter ->setTranslateURIDashes ($ val );
413+ // Need to get or create the AutoRouter instance
414+ $ autoRouter = $ this ->collection ->shouldAutoRoute () ? $ this ->getAutoRouter () : null ;
415+
416+ if ($ autoRouter instanceof AutoRouter) {
417+ $ autoRouter ->setTranslateURIDashes ($ val );
366418
367419 return $ this ;
368420 }
@@ -564,7 +616,7 @@ static function ($match) use ($matches) {
564616 public function autoRoute (string $ uri )
565617 {
566618 [$ this ->directory , $ this ->controller , $ this ->method , $ this ->params ]
567- = $ this ->autoRouter ->getRoute ($ uri , $ this ->collection ->getHTTPVerb ());
619+ = $ this ->getAutoRouter () ->getRoute ($ uri , $ this ->collection ->getHTTPVerb ());
568620 }
569621
570622 /**
@@ -646,8 +698,11 @@ public function setDirectory(?string $dir = null, bool $append = false, bool $va
646698 $ this ->directory = null ;
647699 }
648700
649- if ($ this ->autoRouter instanceof AutoRouter) {
650- $ this ->autoRouter ->setDirectory ($ dir , $ append , $ validate );
701+ // Need to get or create the AutoRouter instance
702+ $ autoRouter = $ this ->collection ->shouldAutoRoute () ? $ this ->getAutoRouter () : null ;
703+
704+ if ($ autoRouter instanceof AutoRouter) {
705+ $ autoRouter ->setDirectory ($ dir , $ append , $ validate );
651706 }
652707 }
653708
0 commit comments