Welcome Guest, Not a member yet? Register   Sign In
How do I retrieve a uri segment in a hook?

Just took a quick look into an old system dir and is_loaded() is a CI2.0 function, doesn't exist yet in CI1.7.2 (both are in common.php and are loaded very early).

Did you try the uppercase version of load_class('URI') instead of load_class('uri')?

[eluser]Salvatore Formisano[/eluser]
yeah, I tried that, I get a white screen in that case..

Are you showing all errors? (like I posted in post #4) Because if "Fatal errors" are shown but load_class('URI') gives you a white screen that means that load_class('URI') gives you a lesser error then load_class('uri'). But without knowing what error it's still giving, it's impossible to tell what's wrong.

EDIT: For example, if you have defined CURRENT_LANGUAGE already somewhere else and forgot to remove that - the second define( 'CURRENT_LANGUAGE', $lang) would throw a parse error which is not shown when error reporting is set to E_ERROR. So set your error reporting to E_ALL to find out what's going on, don't take a white screen for a worse error then a "fatal error" because it isn't. It's just not showing the lesser errors, even though it does crash on them.

[eluser]Salvatore Formisano[/eluser]
Yeah I have

    ini_set('display_errors', 'On');

also when I have

define('CURRENT_LANGUAGE', 'en');

I don't get any error..

I didn't mean it would have to be the define() statement, I used that as an example.

Bottom line still is that errors don't show unless they're fatal or possibly unless they happen after something. I can't tell from this, you'll have to find the mistake. You might try placing a exit() statement directly after fetching the URI - if you see the output: that's not what goes wrong, if you don't that might be what causes the error. But without the actual error you still don't know why.
// Change this
$x = load_class('URI')->segment(1);

// To this:
$x = load_class('URI')->segment(1);
exit ('segment 1: '.$x);

[eluser]Salvatore Formisano[/eluser]

I've tried the example you wrote just now and I only get segment 1: (no $x value basically..)

Thanks for the help anyway, much appreciated

Hmm, that means that it works but that the URI hasn't been parsed yet (and thus segment() returns FALSE). The result is that CURRENT_LANGUAGE is set to FALSE which probably causes the white-screen error later on (because it's expected to be a string).

Here's another thing that might work in its stead:
$segment = substr( load_class('URI')->_parse_request_uri(), 1);
$segment = array_shift( explode( '/', $segment ) ); // takes the first segment
define( 'CURRENT_LANGUAGE', $segment );

[eluser]Salvatore Formisano[/eluser]
Yeah it finally worked!

now my pick_language hook is:

function pick_language() {



  // Lang set in URL
  if(substr( load_class('URI')->_parse_request_uri(), 1))
    $lang = substr( load_class('URI')->_parse_request_uri(), 1);
    $lang = array_shift( explode( '/', $lang ) ); // takes the first segment
    $_SESSION['lang_code'] = $lang;

  // Lang has already been set and is stored in a session
  elseif( !empty($_SESSION['lang_code']))
    $lang = $_SESSION['lang_code'];

  // Still no Lang. Lets try some browser detection then (this happens on the base_url, where no segment is available)
  else if (!empty( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ))
    // explode languages into array
    $accept_langs = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);

    log_message('debug', 'Checking browser languages: '.implode(', ', $accept_langs));

    // Check them all, until we find a match
    foreach ($accept_langs as $lang)
      // Turn en-gb into en
      $lang = substr($lang, 0, 2);

      // Check its in the array. If so, break the loop, we have one!
      if(in_array($lang, array_keys($config['supported_languages'])))

  // If no language has been worked out - or it is not supported - use the default
  if(empty($lang) or !in_array($lang, array_keys($config['supported_languages'])))
    $lang = $config['default_language'];

  // Whatever we decided the lang was, save it for next time to avoid working it out again
  $_SESSION['lang_code'] = $lang;

  // Load CI config class
  $CI_config =& load_class('Config');

  // Set the language config. Selects the folder name from its key of 'en'
  $CI_config->set_item('language', $config['supported_languages'][$lang]['folder']);

  // Sets a constant to use throughout ALL of CI.
  define( 'CURRENT_LANGUAGE', $lang );



and the get_route function which is use is
if ( ! function_exists('get_route'))

    function get_route($route,$parameters=NULL)

      $parameters can be a string if you need to pass just one parameter
      or it can be an array, in which case you have to make sure you pass
      all the parameters in the right order


      if your route is something like


      where the first :num is the blog category id and the second :num is the
      page number

      you would pass


      no regex allowed for this helper, yr
      :any | :num | (:any) | (:num)

      anything else will not work


      // BEGIN

        if(strpos($route,':')) return "get-route-error__did-not-expect-parameters-to-be-null.html";



        if($expected_parameters_n!==$parameters_n) return "get-route-error__n-of-parameters-mismatch-of-expected-parameters.html";

        for( $i=0; $i<$parameters_n; $i++ ) {


      return $route;


thanks a lot for the help man!

Theme © iAndrew 2016 - Forum software by © MyBB