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

[eluser]Jelmer[/eluser]
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')?
#12

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

[eluser]Jelmer[/eluser]
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.
#14

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

Code:
error_reporting(E_ALL);
    ini_set('display_errors', 'On');

also when I have

Code:
define('CURRENT_LANGUAGE', 'en');

I don't get any error..
#15

[eluser]Jelmer[/eluser]
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.
Example:
Code:
// Change this
$x = load_class('URI')->segment(1);

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

[eluser]Salvatore Formisano[/eluser]
Heh,

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
#17

[eluser]Jelmer[/eluser]
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:
Code:
$segment = substr( load_class('URI')->_parse_request_uri(), 1);
$segment = array_shift( explode( '/', $segment ) ); // takes the first segment
define( 'CURRENT_LANGUAGE', $segment );
#18

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

now my pick_language hook is:

Code:
<?
function pick_language() {

  require_once(APPPATH.'/config/language.php');

  session_start();

  // 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'])))
      {
        break;
      }
    }
  }

  // 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
Code:
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

      example:

      if your route is something like

      blog/browse_by_category/:num/page/:num

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

      you would pass

      $parameters=array($blog_cat_id,$page_n)

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

      anything else will not work

      */

      // BEGIN

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

      else
      {
        if(!is_array($parameters)){
          $parameters=array($parameters);
        }

        $expected_parameters_n=substr_count($route,':');
        $parameters_n=count($parameters);

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

        $route=str_replace("(","",$route);
        $route=str_replace(")","",$route);
        for( $i=0; $i<$parameters_n; $i++ ) {
          $pos=strpos($route,':');
          $sub=substr($route,$pos,4);
          $route=substr_replace($route,$parameters[$i],$pos,4);
        }

      }

      return $route;

  }
}

thanks a lot for the help man!




Theme © iAndrew 2016 - Forum software by © MyBB