Welcome Guest, Not a member yet? Register   Sign In
Do we really need a default controller?
#1

(This post was last modified: 01-27-2023, 01:27 PM by iRedds.)

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.
Reply
#2

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.
Reply
#3

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');
$routes->setDefaultMethod('index');

$routes->get('/''Home::index'); 

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
Reply
#4

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 )
Reply
#5

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
Reply
#6

(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.

I don't understand what you say, but maybe there is some bug.
Reply
#7

(01-27-2023, 11:20 PM)iRedds Wrote: Since defined routes are processed first, and then auto routes.

Yes, but I would like to change it so that defined routes routing can also be disabled in the configuration.
Reply
#8

(This post was last modified: 01-28-2023, 07:47 PM by iRedds.)

(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.

I don't understand what you say, but maybe there is some bug.

Router::hanlde();

PHP Code:
public function handle(?string $uri null)
    {
        
// If we cannot find a URI to match against, then
        // everything runs off of its default settings.
        
if ($uri === null || $uri === '') {
            return 
strpos($this->controller'\\') === false
                
$this->collection->getDefaultNamespace() . $this->controller
                
$this->controller;
        } 

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.
Reply
#9

@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.
https://codeigniter4.github.io/CodeIgnit...g-improved

It seems there is no test when $uriProtocol = QUERY_STRING.
Reply
#10

I sent a PR: https://github.com/codeigniter4/CodeIgniter4/pull/7199
Reply




Theme © iAndrew 2016 - Forum software by © MyBB