Hi all,
I am using $this->uri->segment(1); in my navigation view to make the current page's menu item active. However, since CI 3, this doesn't work anymore. other uri functions do still work in the view, such as $this->uri->rsegment(1);
Is this a bug? Is there another way to easily achieve what I'm trying to do?
I don't know what you're trying to do, but CI_URI::segment() works perfectly fine. If it returns NULL, it's because there's no segment to be returned in the first place.
This has nothing to do with views.
Hi @narfbg, segment() returns a specific segment from the url, right? Well, it doesn't. In CI 2, this works in my views, but since CI 3, this doesn't work anymore.
Well, I've tested it on 3.0.x and it works.
Saying that it "doesn't work" in _your_ views doesn't really tell me anything - I don't know how you're trying to use it, for which url, in what kind of a view, etc.
But given that rsegment() works, I'm 100% sure that you're simply not using it properly.
And again - this has no relation to views, that's just a coincidence.
Excuse me, but since this perfectly worked in CI 2 and it doesn't anymore in CI 3, there is reason enough to believe that I'm not doing something wrong. By the way: I'm having this problem in 2 different projects, both on 3.0.6. Simply echoing $this->uri->segment(1) gives no output, on an a page with a url with more than enough segments.
I'd appreciate a little more cooperation from your side.


These are screenshots of my local development environment. I hope this is more than clear enough to you?
We can do this all day ...
narf@vaio /var/www/html/cis $ cat application/controllers/Test.php
<?php
class Test extends CI_Controller {
public function testUri()
{
echo "CI Version:", CI_VERSION, "\n";
echo "Segment 1:", $this->uri->segment(1), "\n";
echo "Segment 2:", $this->uri->segment(2), "\n";
}
}
narf@vaio /var/www/html/cis $ wget -q -O - http://localhost/cis/index.php/test/testuri
CI Version:3.0.6
Segment 1:test
Segment 2:testuri
narf@vaio /var/www/html/cis $
I _know_ that it works.
I know it by default.
I know it because it is unit tested.
I know it because I tested it manually before your last replies.
I know it because I just tested it again with code identical to yours, as shown above.
I would appreciate it if you trusted that I know what I'm talking about instead of assuming that I'm refusing to cooperate. In fact, your very own screenshot disproved something that you had claimed twice already - that this has something to do with views.
By saying that the problem that you're describing is not a CodeIgniter bug, _I am cooperating_ with you - if you accepted that, you wouldn't waste your time trying to look for a problem in the wrong place.
It doesn't matter if you have the same problem in 1, 2 or 500 projects - if it happens with _your_ projects, but not with a completely fresh CodeIgniter setup - it is clearly something in your projects that causes it.
I don't know what it is exactly - addons, extensions, manual patches, your own code - I don't know what you're running, but when I say that I am 100% sure about it - I really am, this is far from the first time that I'm having such an argument.
Excuse me, but I never said this had something to do with views. I was just trying to explain the problem I'm having, in as much detail as possible.
And hey, I do want to believe you, but the problem is this isn't working in the same setup I had in CI 2 vs CI 3. I mainly use CI 3 logic only.
I really wonder what I said wrong to offend you. Either way, this certainly wasn't my intention.
I made a clean CI project in which this does work. So I'm wondering why it isn't working in any of my CI 3.x projects.
The test case I made consists of a simple function in the native CI_Controller. The only code that can still influence this, is autoloaded code.
I really have no clue what I'm missing here...
Okay.. This kept me thinking further... Another piece of code messing with the url, is the following: https://github.com/bcit-ci/CodeIgniter/wiki/uri-language-identifier. It adds a language abbreviation part to the URL.
When using this Language Identifier, $this->uri->segment(0) returns the 2nd segment (which you should get when using $this->uri->segment(2)).

Disabling this Language Identifier, was the solution to this problem.

A small word to @narfbg: If someone posts a bug, it isn't because they are stupid or something. It's because they _might_ have done something wrong or because the code they used (both published by bcit-ci), doesn't work well together. I suggest updating the Language Identifier, since I'm pretty sure I won't be the first with this problem.
FYI: the exact problem is situated around line 87:
$URI->segment(array_shift($URI->segments));
This does something to the segments, while it shouldn't be doing it in my case:
From the config.php:
/* hide the language segment (use cookie) */
$config['lang_ignore'] = TRUE;
When using the Language Identifier with cookies, the URL should be left unchanged. The solution would be something like this:
if(!$lang_ignore) { $URI->segment(array_shift($URI->segments)); $URI->uri_string = preg_replace("|^\/?$lang_abbr|", '', $URI->uri_string); }
(I'm not sure what the rest of the code beneath this does, so this should only be considered a quick fix).
@Rubenvanhoeyveld The excerpt/problem/solution you mention, referring to the wiki is third party code, not part of CodeIgniter. If there is a bug in it, you need to contact the wiki entry creator.
I repeat, this is _not a CodeIgniter bug_.
@Rubenvanhoeyveld If you have problems figuring out where you went wrong, that is appropriate for the forum, not raised as a bug on github.
@Rubenvanhoeyveld ...
First of all, where did I say that somebody, anybody is stupid?
I _could now_ say that suggesting "Language Identifier" changes to me is stupid, because I don't have anything to do with whatever that thing is, but even that wouldn't mean you're stupid - everybody does stupid shit sometimes.
On the other hand, you did say that it doesn't work _in your views_ even after I told you it has nothing to do with them, so you can't really blame me for thinking that it is your claim.
I told you something twice and you just refused to believe it, so the third time I made sure that you do take me seriously. I don't think I've said anything to be reasonably described as offensive, but offense is taken, not given, so I can never be sure about that.
For example, I now want to reiterate on what @jim-parry just said ... This is a bug tracker and as such it is supposed to be used for bugs, not requesting help for whatever problem you may be having. Thus, even if it may be seen as "undecent", as soon as I know that something is not a bug, my job here is done - I can choose to help you and be nice, I can choose to help you without being nice, I can stop replying, I can even tell you to fuck off because it's not my problem anymore.
^ There's nothing inherently offensive about what I'm saying, yet some people will get offended merely by the word "fuck" being used, even though I am only describing an imaginary scenario. Well, all I have to say to those people is this: I don't give a fuck about political correctness. :)
That being said, you've now been wrong about three things here:
Yet, you're still trying to frame me as one in the wrong for something ... Now, being wrong about shit happens, and it's not something to be ashamed of, but I do take offense in being talked down to when I am not wrong. No hard feelings, just stop doing it.
Not sure why you're reacting like that, but I think you must feel really bad. You should talk about your problems and seek help.
Apart from that, I do still think this is an issue tracker and before I knew the problem was linked to the Language Identifier, I had an issue coming from CodeIgniter. So please forgive me for looking for an answer in this ISSUE tracker.
That being said, I will never ever ask another question in this issue tracker ;-). I'll try StackOverflow or something similar instead, where people _do_ want to help out.
I now feel disappointed in CI developers and think I should check out Symfony or other frameworks...
We can't please everyone, sorry.
We did make it quite clear on the repo readme that we use Github issues for BUG TRACKING, and that regular SUPPORT ISSUES are handled on our forum.
Good luck with your future endeavors.
My mental health is just fine, thank you.