Welcome Guest, Not a member yet? Register   Sign In
"is_homepage" function
#1

[eluser]nomikos3[/eluser]
Hi, I need a function to determine if the current url is the homepage. I have not founded any.
If there is one please guide me to it. In the meantime I am using the following:

Code:
function is_home()
{
    @include(APPPATH . 'config/routes.php');

    // takes in account URI routing
    if ($route['default_controller']
            && strcasecmp($route['default_controller'], implode('/', $this->uri->rsegments)) == 0)
    {
        return TRUE;
    }

    // No URI
    if ( ! $this->uri->segment(1))
    {
        return TRUE;
    }

    return FALSE;
}

It is correct or can be improved? Or must I > /dev/null?
THX.-
#2

[eluser]kirkaracha[/eluser]
This is basically the same approach but you don't need to include routes.php and it simplifies the code.

Define a default controller in application/config/routes.php
Code:
$route['default_controller'] = 'Home';

Use URI segments inside the Home controller:

Code:
$segment_1 = $this->uri->segment(1,NULL);

if(is_null($segment_1){
//home page
} else {
//not on the home page
}
#3

[eluser]nomikos3[/eluser]
Hi kirkaracha, but not.

I want to call this function of everywhere, not only in a supposed "Home" controller.
Currently I define this function inside a library, that is reachable of everywhere, so it is OK.

(And if you look better I already take in account $route['default_controller'])

I ask if already exists something similar (and better), otherwise, we could improve this one.

Thanks and regards.-
#4

[eluser]Aken[/eluser]
If you're processing code anywhere else besides the home controller, can't you safely assume that you're NOT on the homepage? Where are you using this code that you need to check for a homepage?
#5

[eluser]nomikos3[/eluser]
[quote author="Aken" date="1311221200"]If you're processing code anywhere else besides the home controller, can't you safely assume that you're NOT on the homepage? Where are you using this code that you need to check for a homepage?[/quote]

Hi.

Yes, Now I understand why you suppose that exists a "home controller". I am using a CI-based CMS called pyrocms. It uses various controllers in a modular architecture. You can define plugins that can introduce functionality of one controller into another one (I hope explain it well). So it is inside of one of these plugins that I need to know if I am in the homepage. These plugins generally output (although not always) data to the "view". You don't want to say "HELLOOO, welcome to my homepage" when you are not, don't you? Confusednake:

Stay in the homepage can occur when you really are in the home controller OR when you are being routed to it. So it is perfectly valid want to know when we are in the homepage.

THX...




Theme © iAndrew 2016 - Forum software by © MyBB