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

[eluser]Salvatore Formisano[/eluser]
As per title,

since my other detailed post isn't being very popular.. ( http://ellislab.com/forums/viewthread/163604/ )


I guess I'll split the questions I have in order to accomplish what I'm trying to do Big Grin


In order to retrieve a uri segment in a controller normally all I have to do is

$this->uri->segment(1)

to get the first uri segment.


How do I do it from a hook tough?
#2

[eluser]WanWizard[/eluser]
Tried
Code:
$CI =& get_instance();
echo $CI->uri->segment(1);
?
#3

[eluser]Jelmer[/eluser]
If your hook is pre-controller, before the Controller (and with it the CI_Base) has been instantiated, the get_instance() won't work.
In that case you could try:
Code:
load_class('uri')->segment(1); // CI 1.7.2
load_class('uri', 'core')->segment(1); // CI 2.0 - needs the second parameter if the URI lib wasn't loaded yet
#4

[eluser]Salvatore Formisano[/eluser]
doesn't seem to be working (I receive a blank page)
#5

[eluser]Jelmer[/eluser]
Set error_reporting( E_ALL ); in your main index and if that doesn't give you the error add ini_set('display_errors', 'On'); just below it.
#6

[eluser]Salvatore Formisano[/eluser]
done, this is what I get

Code:
Fatal error: Cannot redeclare class CI_URI in /Users/macuser/Sites/sitename/system/libraries/URI.php on line 29

(I changed macuser and sitename for privacy, I obviously have the right stuff setup Big Grin )
#7

[eluser]Salvatore Formisano[/eluser]
Even this alone fails (just deleted the rest to make sure the mistake is here:

Code:
<?
function pick_language() {

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

  session_start();

  $x=load_class('uri')->segment(1);
  define('CURRENT_LANGUAGE', $x);
}

?>

and in case this can be useful, here's the hook declaration:

Code:
$hook['pre_system'] = array(
  'class'    => '',
  'function' => 'pick_language',
  'filename' => 'pick_language.php',
  'filepath' => 'hooks'
);
#8

[eluser]Jelmer[/eluser]
Weird, it seems the load_class() function doesn't know the class was already loaded and tries to a second time when already loaded. Which causes your error.

It might be an upper/lowercase issue:
Code:
load_class('URI')->segment(1);

Or you could try something like this (because of the error I'm pretty sure it's already loaded):
Code:
$libs = is_loaded();
$libs['uri']->segment(1); // keys in this one are always lowercase
#9

[eluser]Salvatore Formisano[/eluser]
Heh.. it gets even funnier Smile

Code:
<?
function pick_language() {

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

  session_start();

  $libs = is_loaded();
  $x=$libs['uri']->segment(1); // keys in this one are always lowercase
  define('CURRENT_LANGUAGE', $x);
}

?>

returns

Code:
Fatal error: Call to undefined function is_loaded() in /Users/rino/Sites/gerardopanariello/application/hooks/pick_language.php on line 63
#10

[eluser]Salvatore Formisano[/eluser]
not sure if you noticed.. could this have anything to do with the fact that this hook is pre_system?




Theme © iAndrew 2016 - Forum software by © MyBB