CodeIgniter Forums
=& get_instance in MY_Exceptions - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: =& get_instance in MY_Exceptions (/showthread.php?tid=22816)



=& get_instance in MY_Exceptions - El Forum - 09-21-2009

[eluser]freshface[/eluser]
How can i use $this->ci =& get_instance();


Code:
<?php
class MY_Exceptions extends CI_Exceptions
{
    var $ci;
    
    public function __construct()
    {
        parent::CI_Exceptions();
        $this->ci =& get_instance();
    }

    function show_404($page = '')
    {
    
        //lredirect('_404');
        //redirect('error_404','refresh');  // you can change this with what you want
        $content = $this->ci->db->get_where('pages',array('type' => '404', 'language_id' => LANG_ID))->row();
        if(count($content))
        {
            lredirect($content->slug);
        }
        else // failsave
        {
            $heading = "404 Page Not Found";
            $message = "The page you requested was not found.";

            log_message('error', '404 Page Not Found --> '.$page);
            echo $this->show_error($heading, $message, 'error_404');
        }

        exit;
    }
}

Without getting this error: Fatal error: Call to undefined function get_instance() in /Applications/MAMP/htdocs/albatross/app_frontend/libraries/MY_Exceptions.php on line 9


=& get_instance in MY_Exceptions - El Forum - 09-21-2009

[eluser]InsiteFX[/eluser]
hi,

If your running PHP 4 then it is like this:
Code:
$CI =& get_instance();

Now if your PHP 5 then it is like this:
Code:
$CI = get_instance();

Enjoy
InsiteFX


=& get_instance in MY_Exceptions - El Forum - 09-22-2009

[eluser]freshface[/eluser]
Just tested this but doesn't seem to solve it.
I still got the same error.

I think getting the instance is not available at that time, so i need to load something more, but dont know what.

I hope somebody could help me.

Regards


=& get_instance in MY_Exceptions - El Forum - 09-22-2009

[eluser]Pascal Kriete[/eluser]
Code:
I think getting the instance is not available at that time

Right on the money.

Normally get_instance returns the current controller instance. But since controllers map directly to urls, it doesn't know which one to instantiate.

If you can let us know your overall goal, I'm sure we can make it work (probably by extending the router instead of the exception class).


=& get_instance in MY_Exceptions - El Forum - 09-22-2009

[eluser]freshface[/eluser]
My overal goal is when show_404 is called i redirect to a specific page. (see my code in the first post)


=& get_instance in MY_Exceptions - El Forum - 09-22-2009

[eluser]Pascal Kriete[/eluser]
I saw the code, but it doesn't tell me anything about how you handle non-404 requests.

You get a slug from the db. To me, that means the 404 url is user defined. How do you make sure that the user defined 404 is a valid request? Where does it go?

Or quite simply - what does lredirect do?


=& get_instance in MY_Exceptions - El Forum - 09-22-2009

[eluser]freshface[/eluser]
lredirect is a small helper i created that is language aware.

Code:
function site_lurl($uri = '')
{
    $CI                 =& get_instance();
     $actual_lang         = $CI->config->item('language_abbreviation');
    return $CI->config->site_url($actual_lang.'/'.$uri);
}

function lredirect($uri = '', $method = 'location', $http_response_code = 302)
{
    switch($method)
    {
        case 'refresh'    : header("Refresh:0;url=".site_lurl($uri));
            break;
        default            : header("Location: ".site_lurl($uri), TRUE, $http_response_code);
            break;
    }
    exit;
}



=& get_instance in MY_Exceptions - El Forum - 10-22-2009

[eluser]freshface[/eluser]
Pascal, have you any idee how to use the database class in MY_Exceptions?


=& get_instance in MY_Exceptions - El Forum - 10-22-2009

[eluser]CroNiX[/eluser]
This is the best/easiest implementation I have found to create custom 404 errors and have access to everything that CI can access.


=& get_instance in MY_Exceptions - El Forum - 10-22-2009

[eluser]freshface[/eluser]
I use hmvc, dont know if its compliant, but will check it out.