is_home() function in Codeigniter
Asked Answered
D

6

6

As I know the Wordpress has is_home() function to determine home page.
In YII i use solution like this Yii check if homepage

In CI, templates actually, i faced many times with necessity of it. For example, adding some css classes in tag <body>.

All what i found http://ellislab.com/forums/viewthread/194637/#916899
Can anybody help me or write own solution?

Thank in advance

Devanagari answered 6/5, 2013 at 16:20 Comment(2)
Well, I would suggest defining to yourself what "home" means. Is it a request URI of '/' or some other URI. If so, it should be quite simple to check the request URI to see if it matches.Pulchia
no offence but Laravel makes this silly things so easier.... it probably has the best router mechanism among php frameworks. ;)Dunedin
S
5

You can use $this->router->fetch_class() to get the current controller and $this->router->fetch_method() to get the method too if needed.

So similar to the Yii example you linked to, you could do something like

$is_home = $this->router->fetch_class() === 'name_of_home_controller' ? true : false;

Or to match method too

$is_home = ($this->router->fetch_class() === 'name_of_home_controller' && $this->router->fetch_method() === 'name_of_home_method') ? true : false;

This way even if the page url is http://yoursite.com/ (assuming the home controller+method is the default), http://yoursite.com/home_controller/, http://yoursite.com/something/that/routes/to/home/controller/, etc, as long as it's calling that controller, it'll set $is_home to true.

Edit: Also you can use ($this->router->fetch_class() === $this->router->default_controller) if you don't want to explicitly state the home controller and it's set to the default controller.

Update for CI v3:
$this->router->fetch_class() and $this->router->fetch_method() were deprecated in CI v3. Use $this->router->class and $this->router->method instead.

Selwin answered 6/5, 2013 at 17:2 Comment(0)
C
21

I just wanted to add my answer here as the other method doesn't work with my heavily modified version of CI.

This snippet is what I use to detect if we are on the homepage

if (!$this->uri->segment(1)) {
    // We are on the homepage
}
Cumber answered 14/5, 2014 at 9:15 Comment(0)
S
5

You can use $this->router->fetch_class() to get the current controller and $this->router->fetch_method() to get the method too if needed.

So similar to the Yii example you linked to, you could do something like

$is_home = $this->router->fetch_class() === 'name_of_home_controller' ? true : false;

Or to match method too

$is_home = ($this->router->fetch_class() === 'name_of_home_controller' && $this->router->fetch_method() === 'name_of_home_method') ? true : false;

This way even if the page url is http://yoursite.com/ (assuming the home controller+method is the default), http://yoursite.com/home_controller/, http://yoursite.com/something/that/routes/to/home/controller/, etc, as long as it's calling that controller, it'll set $is_home to true.

Edit: Also you can use ($this->router->fetch_class() === $this->router->default_controller) if you don't want to explicitly state the home controller and it's set to the default controller.

Update for CI v3:
$this->router->fetch_class() and $this->router->fetch_method() were deprecated in CI v3. Use $this->router->class and $this->router->method instead.

Selwin answered 6/5, 2013 at 17:2 Comment(0)
B
1
if($this->uri->uri_string() == ''){ 
echo"You are on homepage"; 
}
Baxter answered 11/11, 2014 at 8:54 Comment(0)
R
1

If you don't have a custom helper create one; otherwise inside any of your custom helper file just paste any of the following code snippets and you should be able to use is_home() from any location in Codeigniter.

function is_home()
{
   $CI =& get_instance();
   return (!$CI->uri->segment(1))? TRUE: FALSE;
}

OR

function is_home()
{
  $CI =& get_instance();
  return (strtolower($CI->router->fetch_class()) === $CI->router->default_controller  && $CI->router->fetch_method() === 'index') ? true : false;
}
Rebut answered 15/9, 2016 at 4:19 Comment(0)
S
1

For better answer you should cover 2 routes for home page:

I assumes there is default controller like this in routes.php:

$route['default_controller'] = 'home';
  • Your home page without default controller name(like: yoursite.com)
  • Your home page with controller name(like: yoursite.com/home)

    if(!$this->uri->segment(1) || $this->uri->segment(1)=='home'){ 
        // YOU ARE ON THE HOME
    }
    
Strode answered 3/11, 2019 at 8:16 Comment(0)
D
0

Why not just simply

public function name_of_home_method()
    {
        $data['is_home'] = true;
    }
Dante answered 29/8, 2017 at 17:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.