Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging behavior of Codeigniter | show_404()

I use a controller to do some custom uri routing for pages, it's currently working great.

Here is a stripped down version of the controller which uses PHPTAL as it's template engine.

public function index()
{
    $this->tal->display('index');
}

public function view($url)
{       
    $this->loadView($url);
}   

private function loadView($url) 
{

   if (file_exists(ROOTPATH . 'webroot/' . $url . '/index.html')) 
   {
       $this->tal->display($url . '/index');
   } 
   else 
   {  
       show_404();
   }
}

The problem

I recently noticed that the following error was appearing in my logs each time the page controller was accessed:

ERROR - 2013-02-06 10:58:23 --> 404 Page Not Found -->

I found this strange as the page displays as expected and there is definitely no 404 header and the network panel shoes no 404 status.

I finally narrowed this down to the show_404() helper method being called in the loadView() method. Removing that line stops the errors from appearing in the log file altogether.

This §show_404()§ should only be executed if the view file cannot be found, in which case it should show the 404 error page, which it does. However the logging part of the method appears to be being executed on every call to the page controller, regardless of whether the loadView() method is called or not.

Example

I access the index() view in the browser, everything appears to work fine, the correct view file is loaded, there are no errors. However a 404 error message is logged from the loadView() method. The loadView() method isn't even being called from the index() method, yet the existence of its output in the log files seems to indicate that it is being executed.

like image 905
Jeemusu Avatar asked Feb 06 '13 02:02

Jeemusu


1 Answers

The default behaviour of show_404 is to insert a log entry when its called. There's a second optional parameter to disable it, so if you just want to show the 404 page without logging it call it like this:

show_404('', false);

The first parameter is the string that would be appended after the --> in the logs, so you can leave that empty too.

As for why the _loadView function might be get called:

If you are using urls without the index.php in them (empty $config['index_page']) then every static asset (image, js, css, etc.) that the webserver server can't find (depends on concrete rewrite rules) will be passed to the php script and probably generate a 404 error there.

like image 83
complex857 Avatar answered Sep 25 '22 12:09

complex857