简体   繁体   中英

How can I load the default controller when class not found

Is it possible to setup CI such that when a URI match is made it follows the standard method of calling the method of the matched class, but when no class is matched, it calls the default class instead?

For instance, lets say I have the following controllers defined:

controllers/main.php
controllers/page1.php
  • When the URI is localhost/Main then controllers/main.php class matches (standard CI)

  • When the URI is localhost/ then the controllers/main.php class matches (assuming default controller is defined in config/routes.php )

  • When the URI is localhost/Page1 then the controllers/page1.php
    class matches (standard CI)

  • When the URI is localhost/Undefinedpage then I want the
    controllers/main.php to be matched.

Note I can't list this explicitly in the config/routes because I don't know what the actual text is.

I also don't want to do wild-card match because it would prevent the normal operation of CI. I also don't want to setup a route for each controller - that would be silly.

A 404 over-ride is close to the operation I want, but the 404-override doesn't prevent a 404 from being issued - so that solution is not valid either.

Thanks for your suggestions.

In config/routes.php

$route['404_override'] = 'controllers/main';

when ever user visits some other URL, CI will ditect that as 404 and redirect to above controller

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM