2

I created a new method to handle with 401 apache error.

My core class extends CI core class but when I call the method name I receive this message:

Fatal error: Call to undefined function show_401() in
G:\Path\application\controllers\loader.php on line 29

class MI_Exceptions extends CI_Exceptions {

    function __construct()
    {
        parent::__construct();
    }

    /**
     * 401 Unauthorized
     *
     * @access  private
     * @param   string  the page
     * @param   bool    log error yes/no
     * @return  string
     */
    function show_401($page = '', $log_error = TRUE)
    {
        $heading = "401 Unauthorized";
        $message = "Unauthorized.";

        // By default we log this, but allow a dev to skip it
        if ($log_error)
        {
            log_message('error', '401 Unauthorized --> '.$page);
        }

        echo $this->show_error($heading, $message, 'error_401', 401);
        exit;
    }
}
4
  • did you try with public function show_401( ? Commented Feb 12, 2014 at 18:11
  • Yep! And that don't work too. Core are autoloaded by CodeIgniter right? Or I need to load core, like I load a lib? Commented Feb 12, 2014 at 19:15
  • @user3108967 Post the controllers/loader.php (Controller) file as well. Commented Feb 12, 2014 at 19:45
  • @Hashem Qolami Just calling show_401(); Commented Feb 12, 2014 at 20:27

1 Answer 1

5

You have extended the Exceptions core class right.

Just make sure that you've set the subclass_prefix config as follows:

$config['subclass_prefix'] = 'MI_';

However it seems you have used the show_401() function directly within your Controller.

The point is that you can't access the class methods as a function in global scope.
In other words, you have to create an instance of the class to access the show_401() method.

But how does show_404() built-in function work?

That's because CodeIgniter has created a Common function for that method which helps the users to use the Exception::show_404() method in global scope.

system/core/Common.php

function show_404($page = '', $log_error = TRUE)
{
    $_error =& load_class('Exceptions', 'core');
    $_error->show_404($page, $log_error);
    exit;
}

The function uses another common function named load_class() to get the instance of the Exceptions core class (returning by reference) in order to access the show_404() method.

Common functions as the same as helper functions, but they're loaded at very beginning of the application.

Getting show_401() function to work

In order to use this function in global scope, you could create a helper function to create an instance of the class, and access you're custom method.

There's no Exceptions built-in helper file in CodeIgniter. Thus, you could create exceptions_helper.php file inside application/helpers/ folder to add your helper function:

application/helpers/exceptions_helper.php

if ( ! function_exists('show_401'))
{
    function show_401($page = '', $log_error = TRUE)
    {
        $_error =& load_class('Exceptions', 'core');
        $_error->show_401($page, $log_error);
        exit;
    }
}

Finally, load the helper file within your Controller:

$this->load->helper('exceptions');

// Then use the function
show_401();

You could also load the helper file automatically (if it's necessary to be loaded always) by adding the helper name into the autoload.php config file:

$autoload['helper'] = array('exceptions'/*, 'language', 'form'*/);
Sign up to request clarification or add additional context in comments.

2 Comments

Could I extend the Common core?
@user3108967 Common.php core file is not a Class and it's not possible to extend that because it is loaded at the beginning of the application. The best approach is using a custom helper file as I suggested :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.