Reputation: 55
Is it possible to turn off the automatic routing in CodeIgniter and have it only process requests if a route for that request exists? Thanks.
Upvotes: 4
Views: 4514
Reputation: 23
Since CodeIgniter v4.2.0, the auto-routing is disabled by default.
You can disable the automatic matching, and restrict routes to only those defined by you, by setting the $autoRoute
property to false:
<?php
// In app/Config/Routing.php
class Routing extends BaseRouting
{
// ...
public bool $autoRoute = false;
// ...
}
// This can be overridden in the Routes file
$routes->setAutoRoute(false);
More info on the CI docs: https://codeigniter.com/user_guide/incoming/routing.html?highlight=autorouting#use-defined-routes-only
Upvotes: 0
Reputation: 107
Codeigniter 4 solution:
app/config/Routes.php at line 24 set value true to false
$routes->setAutoRoute(false);
Upvotes: 0
Reputation: 2809
Keep in mind that Dale's solution:
$route['(:any)'] = "some/default/controller/$1";
only works for one-segment URLs, like:
example.com/foo
but not for:
example.com/foo/bar
You can get around this by using a regular expression instead of the CI wildcard. And by routing to a non-existing class drops a show_404() indeed:
$route['(.*)'] = "none";
Upvotes: 10
Reputation: 14747
Well, another solution could be extends the Router
.
class MY_Router extends CI_Router
.You could override the method _set_routing()
:
This function determines what should be served based on the URI request, as well as any "routes" that have been set in the routing config file.
It should be more complex, but at least can guide you to another solution.
Upvotes: 2
Reputation: 10469
AFAIK you can't turn off CI's automatic routing, but there is a work around:
// you specific routes
$route['admin/(:any)'] = "admin/$1";
$route['search/(:any)'] = "search/$1";
// the catch all route
$route['(:any)'] = "some/default/controller/$1";
Which doesn't actually turn off CI's routing but routes all unmatched uri's to the default controller.
Alternatively you could route to a non-existent controller which I believe will throw the in built 404 error
Upvotes: 3
Reputation: 5731
No, it's not possible turn off the automatic routing convention in CodeIgniter as far as I know, but you can put entries in your .htaccess file to redirect de default routes to the routes you've created.
Upvotes: -1