Do we really need a default controller? |
It's about RouteCollection::setDefaultController() and RouteCollection::setDefaultMethod()
These methods define the controller and method that will be used for the site root. But by default, we already have a route defined for the site root. PHP Code: $routes->get('/', 'Home::index'); Even if autoroutes are used, predefined routes are still checked. In my opinion, specifying the default handler makes no sense. That is, this is another legacy of CI3.
CI4 has three routers.
1. defined routes router 2. auto routes improved router 3. auto routes legacy router In my opinion, it is better these are stackable. The default controller and default method are attributes in auto routes routers. defined routes router does not need them.
I mean that the default controller and method for auto routes is essentially a defined route.
That is, it turns out that this is a duplicate functionality. Since defined routes are processed first, and then auto routes. That is, if we remove the default controller altogether, leaving only the root route, then nothing will change in request processing. It's bad that there is no feedback from the developers to know if they are using the root route, even if the application is focused on auto routes. And, for example, if both are defined PHP Code: $routes->setDefaultController('Home'); For App::$uriProtocol = 'REQUEST_URI' the request will process the route, and for App::$uriProtocol = 'QUERY_STRING' the request will process the default controller. At least that's how it is for me on Windows + spark serve
I always define my own routes, I never use auto routing.
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
Slightly different.
When you use auto routing, the default controller is used when a request is to a directory, and the default method is used when a request does not have a URI segment for the controller method. http://localhost:8080/ --> Home::index http://localhost:80890/subdir/ --> Subdir\Home::index http://localhost:80890/subdir/home --> Subdir\Home::index (01-27-2023, 11:59 PM)kenjis Wrote:(01-27-2023, 11:20 PM)iRedds Wrote: For App::$uriProtocol = 'REQUEST_URI' the request will process the route, and for App::$uriProtocol = 'QUERY_STRING' the request will process the default controller. Router::hanlde(); PHP Code: public function handle(?string $uri = null) If REQUEST_URI is set in the App::$uriProtocol config, then this section of code is ignored, since $url = '/'; But if you set QUERY_STRING, then the condition is triggered. (01-27-2023, 11:57 PM)kenjis Wrote: http://localhost:80890/subdir/ --> Subdir\Home::index It looks wild.
@iRedds Thank you.
If we set App::$uriProtocol to QUERY_STRING, the defined `/` route will be ignored. I think it is a bug. Quote:When no defined route is found that matches the URI, the system will attempt to match that URI against the controllers and methods when Auto Routing is enabled. It seems there is no test when $uriProtocol = QUERY_STRING. |
Welcome Guest, Not a member yet? Register Sign In |