Reputation: 753
As I am very pleased of the command show_404()
which you can call everywhere to show a 404-Error-Page, I did want to implement a show_403()
for requests without permissions.
I created the file application/core/MY_Exceptions.php
and added the following code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Exceptions extends CI_Exceptions {
public function __construct()
{
parent::__construct();
}
public function show_403($page = '', $log_error = TRUE)
{
//do some stuff
echo "test";
}
}
Then I'll call it in a controller application/controllers/Welcome.php
like this:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
// show_404(); // <-- this works!
show_403(); // <-- this works not!
}
}
and I keep getting the following error in the browser, when I access the controllers index method:
Fatal error: Call to undefined function show_403()
As you may have noticed I even tested this on a vanilla installation of CodeIgniter, so you should be able to reproduce this error with just these two files.
I know that I could load the extension manually, but that has not the elegance of using show_403()
wherever and whenever I want...
Routing is set up correctly, CodeIgniter is version 3.0.3, PHP is version 5.6.12., filesystem permissions to application/core/MY_Exceptions.php
have even been set to 777 for debugging purposes.
Upvotes: 1
Views: 428
Reputation: 11987
Your exception class is not loaded, so its giving that error.
Load your class like this
$excep = load_class('Exceptions', 'core', $this->config->item('subclass_prefix'));
echo $excep->show_403();// will echo test
One more suggesion is to use helpers
for this,
Add your code in application/helpers
with file name error_helper
public function show_403($page = '', $log_error = TRUE)
{
//do some stuff
echo "test";
}
Calling your function
$this->load->helper('error_helper');
echo show_403();
Upvotes: 1