CodeIgniter Forums
functions are not seeing uri segments - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: functions are not seeing uri segments (/thread-33095.html)



functions are not seeing uri segments - El Forum - 08-15-2010

[eluser]pransome[/eluser]
I have a function and I want to use one of the uri segments in the function.
The function is not seeing any of the uri segments. Just to test this I made a simple function in a controller that looks like ...

function test() {
$segmentOne = $this->uri->segment(1);
echo 'segmentOne: '.$segmentOne;
}

All that gets rendered is "segmentOne:".
I know that using echo directly in a controller is bad practice - I am just trying to figure out why the controller functions don't see any uri segments.

If I try to use current_url() from the url helper I see the site base url, but no segments.

The url I am using to browse to the function above is http://localhost:8888/RC/index.php/password/test/index - using MAMP

Can anyone help me figure out why I see no segments?


functions are not seeing uri segments - El Forum - 08-16-2010

[eluser]mi6crazyheart[/eluser]
Try this function...
Code:
uri_string()

For more: http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html


functions are not seeing uri segments - El Forum - 08-16-2010

[eluser]pransome[/eluser]
I just tried uri_string() and get an empty string. So basically the same result, the segments cannot be seen. However, if I use the php $_SERVER global and do
echo $_SERVER['REQUEST_URI'];
it renders
/RC/index.php/password/test/index
so the segments seem to be accessible that way.


functions are not seeing uri segments - El Forum - 08-16-2010

[eluser]WanWizard[/eluser]
What happens if you set uri_protocol in config/config.php to REQUEST_URI?


functions are not seeing uri segments - El Forum - 08-16-2010

[eluser]mi6crazyheart[/eluser]
Strange!!!, can u bit show u'r controller code where u's used that "uri_string()"...


functions are not seeing uri segments - El Forum - 08-16-2010

[eluser]pransome[/eluser]
I set $config['uri_protocol'] = "REQUEST_URI"; (have tried all the options here)
and when I browse to "http://localhost:8888/RC/index.php/password/test/index"
with the Controller code below I see:

--- segmentOne:

So somehow the segments are getting lost.

Controller code:

<?php

class Password extends Controller {
function index() {
//echo 'Password';
}

function test() {
echo uri_string();
echo ' --- ';
$segmentOne = $this->uri->segment(1);
echo 'segmentOne: '.$segmentOne;
//echo ' --- ';
//echo $_SERVER['REQUEST_URI'];
}
}


functions are not seeing uri segments - El Forum - 08-16-2010

[eluser]danmontgomery[/eluser]
Are you manually loading the URI library (either in your code or autoload)? It is loaded automatically, and I've run into problems where the segment information is lost if the class is loaded again.

Granted, it shouldn't be a problem loading it twice, I've never looked into why it happens.


functions are not seeing uri segments - El Forum - 08-16-2010

[eluser]pransome[/eluser]
Brilliant! That fixed it.
I did have the library loaded again in my autoload.php file.
Thank you for the help.


functions are not seeing uri segments - El Forum - 08-16-2010

[eluser]WanWizard[/eluser]
Weird.

Just quickly checked it here. When adding 'uri' to autoload, a 1.7.2 installation fails with "Unable to load the requested class: uri". On a 2.0 installation I notice no problems. Neither of them continue with an empty uri_string.


functions are not seeing uri segments - El Forum - 08-17-2010

[eluser]Nalorin[/eluser]
[quote author="pransome" date="1281998034"]Brilliant! That fixed it.
I did have the library loaded again in my autoload.php file.
Thank you for the help.[/quote]

I think pransome means that setting
Code:
$config['uri_protocol'] = "REQUEST_URI";
in config.php was the solution to his problem.

I say this because I was having the same problem and tried fiddling around with loading and autoloading the URL helper, and it was to no avail. It wasn't until I modified the uri_protocal setting above that I saw some results.



Just thought I'd clarify, for the sake of anyone else who, like me: Comes to this thread, finds the post indicating a solution to the problem, and selectively reads posts before it until they find the solution that was provided.