Welcome Guest, Not a member yet? Register   Sign In
Always display custom 404 error page on 404 errors
#11

[eluser]luismartin[/eluser]
Thanks everyone for helping. Sorry Roberto, I misunderstood your solution. I thought you meant you had to edit the show_404() function. Now I see I was wrong.

Anyway, I'm trying to figure it out... how do you get the necessary data (main menu, footer, etc) to display it in the modified default error_404.php page? At this moment I have just copied the output html of the view for the 404 custom controller, and replaced the code in the default error_404.php file. I have tested this way and it works. However this is a bad solution because it's just plain html and any change in the structure, a menu item for instance, will force me to copy and paste again the resulting html of the 404 error controller into error_404.php
#12

[eluser]@robertotra[/eluser]
[quote author="luismartin" date="1328379633"]... I'm trying to figure it out... how do you get the necessary data (main menu, footer, etc) to display it in the modified default error_404.php page? [/quote]

Of course there is always more than a way to do the same thing, but this is what I suggest: inside the file where you prepare the 404 page content (it maybe a controller or a custom library or a helper, it depends by your application structure), put the views inside one or more global variable, as example:

Code:
// this is just an example on how you can organize page content
$data['one'] = 'your 1st data for the view';
$data['two'] = 'your 2nd data for the view';
$data['three'] = 'your 3rd data for the view';
// and so on...

// put your custom view inside a global variable
// use instanceof $CI->load if inside a helper or a custom library
$GLOBALS['404page'] = $this->load->view('your404view', $data, true);

At this point, the global variable will be available inside the 404 page, that might be:

Code:
<?php
// if you put the whole page inside a single variable, you just need
echo $GLOBALS['404page'];

If you use in the 404 page the same heading and footer than the normal pages, split the page content into more views and put the headings preparation outside the main controller, inside a helper or a class that can be loaded by both controllers (main and error). Then the final 404 page might be something like that:

Code:
<?php
echo <<< STOP
{$GLOBALS['heading']}
<p class='warnBox'>
  <h2 class='warnTop'>{$GLOBALS['title']}</h2>
  <h3 class='warnMex'>{$GLOBALS['message']}</h3>
</p>
{$GLOBALS['footer']}
STOP;

I suggest also to put before the echo statement some basic check to avoid that a variable is empty, setting some default content if anything went wrong (so you can check that the process was ok)

Roberto

#13

[eluser]luismartin[/eluser]
Thanks again! I'll do it that way.

Cheers!
#14

[eluser]@robertotra[/eluser]
Glad to help.
Just a note: I have edited my previous message because in the rush to reply I put formatted HTML inside variables to pass to the view, which is logically against MCV pattern, even if it does not cause any php error or warning. Maybe you want to see it again, just for clarity.
Have a nice coding ;-)
Cheers
R.




Theme © iAndrew 2016 - Forum software by © MyBB