0
votes

I think I'll become a bald because of pulling my hairs...

According to CodeIgniter tutorial and also my previous experience, I can easily determine if 2nd, 3rd or 4th segments exist or what are their values.

if($this->uri->segment(3)) {
     //do something
}

or

if($this->uri->segment(3)=="foo") {
     //do something
}

Everything is okay for ALL other segments except the FIRST SEGMENT!

I can not use any conditional statements for FIRST SEGMENT!

if($this->uri->segment(1)) does not work. Because it always returns true. But I want to check if first segment exists or NOT. When I browse to my site, because of removing index.php, it looks like www.domain.com . All other controllers goes like domain.com/firstcontroller, domain.com/secondcontroller

BUT I need to create a condition for the WEB ROOT.

I tried __construct() function but it didn't work neither.

The thing I want to do is: I will show some HTML if current page is NOT webroot (even without 1st segment) or it's not my main controller.

Can you help please?

EDIT: Errr... I'm sorry. I've figured out the problem. It was about using correct operator. I was supposed to use XOR instead of OR. So there is no problem with determining $this->uri->segment(1) ... It's just same as other segments...

4

4 Answers

0
votes

Have you tried type casting before running the function?

E.g

$seg = (int) 1; $this->uri->segment($seg);

Or you could try and grab the data from

$seg_array = $this->uri->segment_array();

and then just examine the returned array.

0
votes

Not sure why you need this for and from where you would call it? The first segment is always the name of the controller executed, the second segment is the name of the function from that controller that is executed. If you haven't change the default routing.

$this->uri->uri_string() will return you the controller/method.You might also want to check $this->uri->ruri_string() and find more info here. Hope this helps you.

0
votes
if ($this->router->class != $this->router->default_controller) {
    // Run stuff here
}

Try this yourself: print_r($this->router)

You can be extra sure through:

if ($this->router->class != $this->router->default_controller or $this->router->directory or $this->router->method != 'index') {
    // Run stuff here
}
0
votes

I was supposed to use XOR instead of OR inside my conditional function. So there is no problem with determining $this->uri->segment(1) ... It's just same as other segments...