Welcome Guest, Not a member yet? Register   Sign In
Controller Based 404
#11

[eluser]theQco[/eluser]
[quote author="pyromaniac" date="1228760673"]Extend the 404 function in the Extention class, much easier.

Use get_instance() and load the url helper to use base_url();[/quote]

I was using this method with success until I just realized that if I get a 404 from my base_url then get_instance() is an undefined function.

For instance, if I go to localhost/mysite/controller/bad_function - it correctly sends me to my 404 controller. But if I go to localhost/mysite/bad_function - I get the undefined function Fatal Error.

I did some debugging and found that if it comes from the first, then the 'base' classes are all initialized. But if I go to the second one, then only Config, Hooks and URI class are the only ones initialized.

Any thoughts? This was the best method so far for me but I need it to work on all 404s.
#12

[eluser]sophistry[/eluser]
hi Q:

let me see if i follow you by restating your problem.

you are talking about two things...

1) bad function name
2) bad controller name

you say you can handle bad function name by extending the Exception class, but bad controller name is handled by CI too early and bails on you before your custom 404.

you could try making a route that handles anything and point it to a parent controller that implements the site_migrate technique i mentioned above: http://codeigniter.com/wiki/site_migrate/

then you handle your 404s right in the base controller - that way everything you need is loaded already and you don't have to extend any CI classes.

cheers.
#13

[eluser]zippy[/eluser]
Careful now...

You must be sure to preserve the duff URL so the 404 header maps to it. As you probably realise, at all costs you must avoid redirecting to a special 404 page with its own URL. If for no other reason, it's all-too-easy to inter alia get your 404 page URL indexed in the SEs. How sucky is that? Unless, of course, your 404 page is the most-visited page on your site (yes, there are some) in which case you might want to monetize it with Adsense or similar. ;-)

AFAIK, an empty REQUEST_URI gets mapped to the default method of the default controller. If you want any other method of the default controller you have to explicitly specify the default controller's name in segment[0] followed by the other method name in segment[1].

It's been a while since I last visited this issue. The increasingly arcane methods I've seen suggested to work around it have all ..er ..had weaknesses?

The presumption that URLs can best be resolved by some magical naming convention is a flaw in the core logic. For every URL that's valid, there's an infinite number that are not. Most PHP frameworks have this problem, and mostly because they pander to PHP 4.x.

An XML virtual structure would be far more reliable and easier to maintain.

Some might debate that, but WTF?
#14

[eluser]theQco[/eluser]
Thanks for the quick responses guys.

@sophistry - I started looking around at using routes and site_migrate but it seems sort of overkill for me.

Maybe this might help:

All I am looking to do is get the URI that the user tried accessing that caused the 404. I have $page inside of show_404 but is there a way to get it to my controller without passing page through the URL? If not, it's fine, I can just URL/$page it but I would like to keep it as clean as possible.
#15

[eluser]zippy[/eluser]
Pardon me for farting in the Church of the Framework, but $_SERVER['REQUEST_URI'] is the global you want.
Check out php.net for intimate details.
#16

[eluser]theQco[/eluser]
[quote author="zippy" date="1231210181"]Pardon me for farting in the Church of the Framework, but $_SERVER['REQUEST_URI'] is the global you want.
Check out php.net for intimate details.[/quote]

Not a bad thought. I tried that at first as well. The problem is that once I hit show_404, I am using header() to go to my controller. Because of that request_uri is no longer the page that caused the 404.

Am I doing something wrong?
#17

[eluser]zippy[/eluser]
No. It's CI that's f*ck*d. For all the reasons above.
#18

[eluser]sophistry[/eluser]
cookie?
#19

[eluser]zippy[/eluser]
With a coffee to dunk it in?

Seriously, I love CI. I just hate how messed up it is below decks.

It'll change, everything does.
#20

[eluser]sophistry[/eluser]
@Q... i guess i don't understand what you are trying to do.

you want to issue a 404 'not found' header and *then immediately* do a CI redirect() to a controller?
but, also keep track of the URL that triggered the 404 in the first place?

is that right?




Theme © iAndrew 2016 - Forum software by © MyBB