routing and home page controller

edited April 2012 in Modules
I've begun to notice some ugly PHP errors when navigating to urls where a controller exists, but a method doesn't.

I think this is because I changed $route['default_controller'] = 'fuel/page_router'; to use a controller named "home". With the page_router controller in place, the urls pointing at non-existent methods resolve nicely to a 404, but using a controller you can end up with PHP errors (originating in Fuel_hooks.php). One solution would be to arrange some routing or controller checks for non-existing methods and point these at the 404. But that seems a bit manually intensive (ie every time you make a new controller).

Ideally I'd have a situation where method-less urls 404'd, AND there would be a default home controller (responding to "/" & "/home"). I should also add that I use the CMS to inject data into controllers where useful such that there is a CMS page "home" that maps onto a controller "home" that pulls in a view that combines blocks alongside the CMS values. To achieve this I have fuel_mode set to auto, and do some chicanery by emptying fuel_set_var('layout', '') and adding fuel_var('body', '') in the view.

The only way I can think of having my cake and eating it too is to have a basic "home" view that redirects (301) to the controller /home - except this won't work with fuel mode set to auto, because it fetches from the DB first and presents the home page CMS content in the plain vanilla layout. Yet I need "auto" to achieve the controller / CMS mix I outlined earlier.

Any suggestions would be very welcome! It occurs to me that maybe extending the page_router to handle the opt-in Fuel methodology except for the home page is a possibility, but I'm not sure how to achieve this.

Comments

  • edited 9:52PM
    Could you provide some basic steps to replicate the problem so I can test it out on my end to see what I can find?
  • edited April 2012
    Changed default controller to home, create a home::index() controller method which loads a view.

    These use the default:
    http://localhost/site/
    http://localhost/site/home
    http://localhost/site/home/index

    404's
    http://localhost/site/eubhfvubhfeubh

    Errors:
    http://localhost/site/home/eubhfvubhfeubh

    Undefined index offset 1 hooks/Fuel_hooks.php #48

    Fatal error: require_once(): Failed opening required '/Library/WebServer/Documents/wildbamboo/fuel/application/../modules/fuel//controllers/.php' (include_path='.:') in /Library/WebServer/Documents/wildbamboo/fuel/modules/fuel/hooks/Fuel_hooks.php on line 48

    When you don't change the default_controller and leave it as fuel/page_router the errors don't show but it doesn't attempt to load the home controller. It will however load the home view by default.

    Of course a _remap() in the controller catches the missing method, too manual?
  • edited 9:52PM
    1) application/config/my_fuel.php
    fuel_mode = 'auto';

    2) application/config/routes.php
    $route['default_controller'] = 'home';

    3) application/controllers/home.php
    class Home extends CI_Controller { function __construct() { parent::__construct(); } function home() { parent::Controller(); } function index() { $data = array(); $data['view'] = 'home/index'; $page_init = array('location' => 'home'); $this->load->module_library(FUEL_FOLDER, 'fuel_page', $page_init); $this->fuel_page->add_variables($data); $this->fuel_page->render(); } }

    4) create view for home (optionally include fuel_var('body',''))
    5) create CMS page "home"
    6) navigate to url /home/foo

    At this point I get:
    A PHP Error was encountered Severity: Notice Message: Undefined offset: 1 Filename: hooks/Fuel_hooks.php Line Number: 48 A PHP Error was encountered Severity: Warning Message: require_once(C:/wamp/www/fuelcms/fuel/application/../modules/fuel//controllers/.php) [function.require-once]: failed to open stream: No such file or directory Filename: hooks/Fuel_hooks.php Line Number: 48

    7) reset default_controller to fuel page_router
    8) hit /home/foo again (it's OK now, it's a 404)

    So I can see that page_router is sanitizing non-method urls, but I'm not sure what the next best step would be. Incidentally, the same happens regardless of the controller path / name, it would occur for /news/foo as for /events/foo. It seems to be a recognised Codeigniter issue.
  • edited 9:52PM
    Try changing the pre_controller hook in the Fuel_hooks to the following:

    // this hook allows us to route the the fuel controller if the method // on a controller doesn't exist... not just the controller itself' function pre_controller() { // if called from same Wordpress, the the global scope will not work global $method, $class, $RTR; $class_methods = get_class_methods($class); // for pages without methods defined if ((isset($class_methods) AND !in_array($method, $class_methods) AND !in_array('_remap', $class_methods)) AND !empty($RTR->default_controller)) { $fuel_path = explode('/', $RTR->routes['404_override']); if (!empty($fuel_path[1])) { require_once(FUEL_PATH.'/controllers/'.$fuel_path[1].'.php'); $class = $fuel_path[1]; } } }
  • edited 9:52PM
    Yes that fixes it completely - at least in 2 recent exports of Fuel that I have. In one that dates back to September, it still breaks, but there must be some other issue there that's now not relevant.
  • edited 9:52PM
    OK... that's code scheduled for the next release.
Sign In or Register to comment.