Check if a page exists

edited September 2012 in Modules
Hello!

I am trying to develop a localization feature for my fuel cms installation.
Basically I want to serve different versions of a page depending on the user's location (like Global/US/Europe).

The idea is to redirect the user to the localized page, if there is one, for the requested url.
For example when an user in Europe access example.com/contact:
> check if the contact-eu page exists (at least in the cms, cms + view would be cleaner)
> yes > redirect to example.com/contact-eu
> no > render example.com/contact

I'm currently trying to extend the Page_router->_remap() function in MY_Controller to do the test and redirection there.
I can see that the page_router sends the 404 at line 155 when no page is found ($output === FALSE) but I can't really nail the moment where the test is done.

Any thoughts? Thanks!

Comments

  • edited 4:16PM
    I'd perhaps look into creating a post_controller_constructor hook so as not to modify the page_router controller if you don't have to:
    http://codeigniter.com/user_guide/general/hooks.html

    Some example code may look something like the following (not I hard coded the location value but you will probably want to make that dynamic):
    $CI =& get_instance(); $vars = array('my_var' => 'test'); $page_init = array('location' => 'contact-eu'); $CI->load->module_library(FUEL_FOLDER, 'fuel_page', $page_init); $CI->fuel_page->add_variables($vars); $output = $CI->fuel_page->render(TRUE); if (empty($output)) { $page_init = array('location' => 'contact'); $CI->fuel_page->initialize($page_init); $output = $CI->fuel_page->render(TRUE); } echo $output; exit();
  • edited September 2012
    Thanks a lot!
    Somehow the second time I initialize fuel_page I always get the following error along with a 404 page.

    Undefined index: location Filename: libraries/Fuel_page.php Line Number: 149 Undefined index: layout Filename: libraries/Fuel_page.php Line Number: 150

    I'm really not sure of what's happening, I can only see that `while(count($segments) >= 1)` directly breaks at line 122 and therefore the $page_data array is incomplete later.


    Anyway, I found that simply calling $CI->pages_model->find_by_location($location, true); is enough to tell if a page exists in the database and for now it will be enough for me.

    As you suggested I ended up created a post-constructor hook which is more clean.
    I then test if the current controller is the Page_router (no need to mess with static controllers), redirect to the localized page if it exists or simply return otherwise.

    Here is my code if it can be useful to someone:
    function filter_location() { $CI =& get_instance(); //Exit if regular controller if (!is_a($CI, 'Page_router')) return; //Get requested page $requested_page = $CI->uri->uri_string; //Add home prefix to home page if ($requested_page == '/') $requested_page = 'home'; //Get current localization $CI->load->library('localization'); $localization = $CI->localization->get_code(); //No processing for global version if ($localization == Localization::global_loc) return; //Check if not already a correctly localized page if (strrpos($requested_page, "-$localization") >= 1) return; //Check if localized page exists //$page_init = array('render_mode' => 'auto', 'location' => "$requested_page-$localization"); //$CI->load->module_library(FUEL_FOLDER, 'fuel_page', $page_init); //$output = $CI->fuel_page->render(TRUE, FALSE); $CI->load->module_model(FUEL_FOLDER, 'pages_model'); $page = $CI->pages_model->find_by_location("$requested_page-$localization", true); //If page doesn't exist return and give hand to Fuel's Page_router->_remap function if (empty($page)) return; //Page exists, redirect to localized page redirect("$requested_page-$localization"); }
Sign In or Register to comment.