Redirecting to 404

edited September 2016 in Bug Reports
I have a question... my site has a combination of CMS pages and controller pages. Now I want to establish a custom 404 page, because on calling a non existing page at the moment the CMS tries to build a page. I have debugged the thing and the call is not routed into my layout libraries. The strange thing is that the page loads my header.php but no file where I have included that file is not run through...

Do you have a hint for me where to start debugging?


  • edited 12:08PM
    Hmm got a little bit closer... via debugging I ended up in the library Fuel_pages initialize. There a page is created with the default layout main...
    Have I configured something wrong? From my understanding the 404 override should have happened earlier...
  • By default, Fuel handles the 404 and presents the ./views/_layout/404_error.php

    Put your custom code in there.

    The 404 route is configured in ./config/routes.php
  • edited 12:08PM
    aaaaaaaaaah thanks... that was the missing link...
    config/routes.php says $route['404_override'] = 'fuel/page_router';


    And I am wondering why it results in the page router :-)
  • edited 12:08PM
    hmmm I have now a controller Error
    <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); class Error extends CI_Controller { public function error404() { $this->fuel->pages->render("404notfound", $data, array('render_mode' => 'cms')); } } /* End of file error.php */ /* Location: ./application/controllers/error.php */ ?>

    and my routes.php says
    $route['default_controller'] = 'homepage'; $route['404_override'] = 'error/error404'; $route['admin'] = 'fuel';

    On calling a non existing url I get the error
    An Error Was Encountered
    Unable to load your default controller. Please make sure the controller specified in your Routes.php file is valid.
  • edited 12:08PM
    What if you change the method name of "error404" to just "index" and then change the route to:
    $route['404_override'] = 'error';
  • edited 12:08PM
    hmmm ok. Now the controller is found. But that was not the whole trick...
    I have 2 layouts in my CMS. The main layout for simple cms content pages and a module layout for more custom pages generated on base of simple modules in the cms.

    All pages now are redirected to the error page, although they are existing in the cms.
  • edited 12:08PM
    I debugged the routing... File Router.php in modules/fuel/core method _validate_request
    $this->locate does not return anything for the given segments, because the cms routes are not loaded into $this->routes
  • edited 12:08PM
    If you overwrite the 404_override route, then all non-controller pages will get routed there instead which means all pages in the CMS will need to be rendered via another controller.
  • edited 12:08PM
    I solved the problem for my installation by creating a my_page_router controller and overriding the _remap function.

    if (!array_key_exists(uri_path(TRUE), $this->fuel->pages->cms()) && uri_path(TRUE) != "offline") { $this->location = "404notfound"; } else { $this->location = uri_path(TRUE); }

    For my installation this works, because the offline page is the only page generated in view_mode.
  • edited September 2016
    not array_key_exists but in_array must be used.
    But this is not the whole solution... this does not take into account urls with a / at the end and the module detail pages with the id as suffix are not recognized correctly...

    I think I need to spend some more thoughts into this ...
    I want to achieve the following:
    1. all non existing pages should be redirected to 404 cms page
    2. all existing pages should be recognized no matter if the url is http:///page or http:///page/
    3. all non existing detail pages (if the user enters a non existing ID into the url) should produce a readable result

    I think 3. can be achieved in the models of the corresponding model... am I right?
  • edited 12:08PM
    Ok... I think I got 1 and 2 with
    if (!in_array(uri_path(TRUE), $this->fuel->pages->cms()) && uri_path(TRUE) != "offline") { $uri_path = uri_path(TRUE); if (substr($uri_path, -1) == "/") { $uri_path = rtrim($uri_path, "/"); } elseif (is_numeric(substr($uri_path, strrpos($uri_path, "/") + 1))) { $uri_path = rtrim($uri_path, substr($uri_path, strrpos($uri_path, "/"))); } if (!in_array($uri_path, $this->fuel->pages->cms())) { $this->location = "404notfound"; } else { $this->location = uri_path(TRUE); } } else { $this->location = uri_path(TRUE); }
    in the file my_page_router function _remap($method).
  • edited 12:08PM
    If the detail pages are generated from a model's data, you can look at the module's post pages example:

    Note that in the example there is a 'empty_data_show_404' parameter you can use to
  • edited 12:08PM
    I am not using the generated post pages at them moment...
    my module config looks like
    $config['modules']['missions'] = array( 'module_name' => 'Einsätze', 'instructions' => lang("einsatz_instructions"), 'permission' => 'missions', 'item_actions' => array('save', 'view', 'publish', 'activate', 'delete', 'duplicate', 'create') );
    The overview and detail page are generated via the layout _module.php

    Is a usage of the empty_data_show_404 parameter also possible in this case?
  • edited 12:08PM
    That parameter is only applicable to post pages. It sounds like in your case you'll need to add some code in your layout_model.php file to display content if no model record exists.
  • edited 12:08PM
    the problem was that the fuel redirect for 404 expects a page named 404_error in the cms. My paged was named different...
Sign In or Register to comment.