0

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?

Mitya
  • 33,629
  • 9
  • 60
  • 107
  • why not -`$route['default_controller'] = catchall_controller';` ? – splash58 May 17 '16 at 13:06
  • That's being used, to ensure that going to `www.domain` loads the homepage. I suppose I could set a catch-all default controller that, if no argument is passed, loads the homepage. Just thinking out loud here. – Mitya May 17 '16 at 13:09
  • What version of Codeigniter are you using? – CodeGodie May 17 '16 at 13:13
  • so if you have this "catch all" controller, would you still need a 404 page? if its catching all requests, then wouldn't 404 be obsolete? – CodeGodie May 17 '16 at 13:29

3 Answers3

2

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;
CodeGodie
  • 12,116
  • 6
  • 37
  • 66
  • 1
    Would that still send a 404 header, though? I got round the issue by having a "gatekeeper" default controller that bounce to the homepage *unless* a token was passed. +1, though. – Mitya May 17 '16 at 13:55
  • 1
    it will not send a 404 header unless you specify it. – CodeGodie May 17 '16 at 13:57
0

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.

sotoz
  • 3,100
  • 3
  • 34
  • 41
  • I think this might not be what hes looking for. The point is not to create an entire list of routes, just one "default" or "catch all" route. – CodeGodie May 17 '16 at 13:20
  • Thank you for your help. I agree with the commenter, though. – Mitya May 17 '16 at 13:53
0

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

$route['(.*)'] = 'catchall_controller';
Oldskool
  • 34,211
  • 7
  • 53
  • 66
Yaseen Ahmad
  • 1,807
  • 5
  • 25
  • 43
  • 2
    why would this work? provide an explanation. How does this differ from `$route['(:any)']` ? – CodeGodie May 17 '16 at 13:28
  • 1
    in codeigniter 2 the (:any) work for all parameters but in codeigniter 3 this is change see in this to pass perimeters http://stackoverflow.com/questions/37111876/unlimited-parameters-get-last-one/37113315#37113315 – Yaseen Ahmad May 17 '16 at 13:38
  • Thank you for your help. – Mitya May 17 '16 at 13:53