Welcome Guest, Not a member yet? Register   Sign In
How can i access to database in error pages ?
#1

[eluser]Ali Fattahi[/eluser]
Hi
i want to access database library to use database in error pages
how can i load database in error pages ?

Best Regards
Ali
#2

[eluser]Maglok[/eluser]
Hey there,

Error pages can be a bit tricky. You should first get the CI instance then load it I think. Kinda like this:

Code:
<?php header("HTTP/1.1 404 Not Found");

if (function_exists('get_instance')) {
    
    $CI= &get;_instance();

    // Do database stuff

    $CI->load->view('errors/404', $data);

} ?>
#3

[eluser]Ali Fattahi[/eluser]
[quote author="Maglok" date="1291300934"]Hey there,

Error pages can be a bit tricky. You should first get the CI instance then load it I think. Kinda like this:

Code:
<?php header("HTTP/1.1 404 Not Found");

if (function_exists('get_instance')) {
    
    $CI= &get;_instance();

    // Do database stuff

    $CI->load->view('errors/404', $data);

} ?>
[/quote]

Hello Maglok
Thanks to reply
i tried blow code but get_instancse doesn't exist in Error Pages Sad
Code:
if (function_exists('get_instance'))
{
    $CI= &get;_instance();
}
else
{
    echo ('Error !');
}
#4

[eluser]Maglok[/eluser]
Hmm odd. I have done this quite some time ago so I am a bit rusty on it.

The concept whirls around that in the error404.php in the /errors dir you do not have access to CI stuff. So you gotta somehow end up loading a view and do whatever you want from there.

I looked through my code where I do that, but I can't seem to find anything that wouldn't make the code snippit work.
#5

[eluser]Ali Fattahi[/eluser]
[quote author="Maglok" date="1291302166"]Hmm odd. I have done this quite some time ago so I am a bit rusty on it.

The concept whirls around that in the error404.php in the /errors dir you do not have access to CI stuff. So you gotta somehow end up loading a view and do whatever you want from there.

I looked through my code where I do that, but I can't seem to find anything that wouldn't make the code snippit work.[/quote]

Thanks you for your attention Smile
it seems there isn't way to access database in error pages Big Grin
i am writing a huge project with CI and it must save logs in all over the project such as error pages uri and etc in database . And database loading in error pages is my problem Smile
Anyway thanks for yor attention
#6

[eluser]Maglok[/eluser]
Ah I got it Smile

This app I got going is a CMS-like thing. It checks if a url is a controller, otherwise it throws it as a parameter and sees if it is a page in the database. If it isnt, it throws an error.

What you gotta do is extend the Router class to not end in:
Code:
// Can't find the requested controller...
        show_404($segments[0]);

You can then direct the app to whatever you want basically. So make a MY_Router and change the core functionality. Bit sloppy I'd say, but it works.

See if you call show_404() in a controller it should be able to access the CI instance, while in the Router it seems to not be able to. So I got a content.php which checks if a page exsists, if not it throws a 404 and then the get_instance works.




Theme © iAndrew 2016 - Forum software by © MyBB