Welcome Guest, Not a member yet? Register   Sign In
Default Routing if page not found
#1

[eluser]Simon B[/eluser]
Hi All,

I was wondering if someone could tell me if there is a graceful way of redirecting requests to my Home Page if the uri does not match a valid controller/method instead of showing the 404 error page?


Thanks in advance,
Simon
#2

[eluser]dioony[/eluser]
hi!

imho there a three possibilities:
1. you change the application/errors/error_404.php to redirect to you're default-page
2. you override (extending the core) the exception-file (create a MY_Exceptions) and override the show_404()-method...
3. afaik the routes from the routes-config will run in order they are defined. therefore it could be possible to put a wildcard which excepts every uri at the end of all you're routes... (but i don't know if this will work...)

i prefer the second solution. its more overhead, but it wouldn't destroy the dry-ci-concept...
#3

[eluser]Simon B[/eluser]
Thanks dioony for you quick reply!

I was half way throught implementing your second option as you posted! Yes I agree this is the preferable option. I also considered creating a MY_Routes class but this seemed to extreme.

Thanks again,
Simon
#4

[eluser]louis w[/eluser]
Only one of the methods in the Routes.php files deals with 404ing. I can not remember the name of it offhand, I am not in front of my installation. Couldn't you make a MY_Routes but only bring over the one method you want to change.
#5

[eluser]Simon B[/eluser]
Hi louis,

Yes, the method i found was called _validate_segments().

However, I found that I only required this functionality for one of my clients and the fact that I have many sites running of the same CI system installation, I thought it best to minimise the scope of the possible impact to just the one client.

Cheers,
S
#6

[eluser]louis w[/eluser]
You have more then one site running inside one application folder?
#7

[eluser]Simon B[/eluser]
No, I have one CI system installation but have multiple application folders. Quite simple to setup: place the index.php at the 'root' of the application directory structure and set the APPPATH and BASEPATH accordingly. :-)
#8

[eluser]louis w[/eluser]
So then what is the problem with making a custom Router.php. Make a MY_Router.php and place it in your application/libraries folder.
#9

[eluser]Simon B[/eluser]
I suppose it comes down to personal preference and application architecture/design in the end.

For me MY_Router would have a much larger scope of impact rather then overriding 404 of the exceptions and thus the possibility of affecting things on a much greater scale if don't code it correctly and especially when the CI_Router is a VERY important core library (not that I will ever admit to writing code that has bug in ;-))




Theme © iAndrew 2016 - Forum software by © MyBB