Nested Prefix routing => ....Controller could not be found

There is written:

When defining prefixes, you can nest multiple prefixes if necessary:

As example is shown:

$routes->prefix('Manager', function (RouteBuilder $routes) {
    $routes->prefix('Admin', function (RouteBuilder $routes) {
        $routes->connect('/{controller}/{action}');
    });
});

In my case this had to be:

    $routes->prefix('Backend', function (RouteBuilder $routes) {
        $routes->prefix('Admin', function (RouteBuilder $routes) {
            //$routes->connect('/{controller}/{action}'); //=> errormessage: BackendController could not be found.
            $routes->fallbacks(DashedRoute::class); //works
        });
    });

If I use (inside of $routes->prefix…)":

$routes->connect('/{controller}/{action}');

I get the error “BackendController could not be found.”

If I use

$routes->fallbacks(DashedRoute::class);

I get the wanted page displayed as expected.

Do I need to change something?

What URL are you trying to get to?

Thanks for your reply

I try to reach

http://192.168.178.39/termbase/customer/admin/users

where 192.168.178.39/termbase is the domain

and routing is:

$routes->prefix('Customer', function (RouteBuilder $routes) {
        $routes->prefix('Admin', function (RouteBuilder $routes) {
            $routes->connect('/{controller}/{action}'); //=> errormessage: ...Controller could not be found.
            //$routes->fallbacks(DashedRoute::class); //works
        });
    });

The error message is:

CustomerController could not be found.

I mostly just use explicit and fallback routes, not much in the way of parameterized ones like this, so take this with a grain of salt. But it seems to me that the URL you’re trying to access won’t match the route you’ve specified here because there is no action parameter.

2 Likes

Yeah. I would recommend reading up what fallbacks() does for you.
If you want to avoid that method, you need to replicate it properly.

1 Like

You’re both right, and thank you for the advice. I’m still having a bit of trouble with the routing at the moment, but that’s due to my poor English skills.