Mitya
Mitya

Reputation: 34556

Code Igniter: default controller for unrouted requests

I have a CI setup where a URL may invoke a specific controller OR should be forwarded to a catch-all controller where no such controller exists. Sort of like default in a switch statement. Examples:

domain/real-controller          //<-- handled by controllers/Real-controller.php
domain/another-real-controller  //<-- controllers/Another-real-controller.php
domain/foobar                   //<-- no such controller; forwarded to a catch-all

I'm aware of rerouting, but I can't do

$route['(:any)'] = 'catchall_controller'

as this will (presumably) block reqeusts to legitimate controllers.

I could presumably do something hacky with 404 handling, but I wondered if there was a better way. Anyone know one?

Upvotes: 0

Views: 102

Answers (3)

Yaseen Ahmad
Yaseen Ahmad

Reputation: 1797

In codeigniter 2 the (:any) works for all parameters, but in codeigniter 3 this is changed. Change your route to:

$route['(.*)'] = 'catchall_controller';

Upvotes: 0

CodeGodie
CodeGodie

Reputation: 12132

Since this controller is a "catch all", then it is pretty much doing what a 404 page would do. In that case, you can do this in your routes:

$route['default_controller'] = 'welcome';
$route['404_override'] = 'catchall_controller';
$route['translate_uri_dashes'] = TRUE;

Upvotes: 2

sotoz
sotoz

Reputation: 3098

You CAN use $route['(:any)'] = 'catchall_controller' but you MUST put it on the end of your routes.php file :).

Therefore, every other router/controller can be fulfilled before going to the last line that has your catchall_controller.

Upvotes: 0

Related Questions