简体   繁体   中英

CodeIgniter: How to match current_url against views url (to construct a nav)

In order to apply the active class to the current item in the navigation menu. I need to check if its link is equal to the URL

I am trying like this:

<ul class="nav">
    <li class="<?php if(uri_string(current_url()) == base_url() || uri_string(current_url()) == '') echo 'active'; ?>">
         <a href="<?php echo base_url() ?>">Home</a>
    </li>
    <li class="<?php if(uri_string(current_url()) == base_url('news/create/')) echo 'active'; ?>">
        <a href="<?php echo base_url('news/create/') ?>">+ New</a>
    </li>
</ul>

Wich seems to work fine with the home

But in news/create won't ever be equal....

It compares news/create ( uri_string(current_url() ) with /news/create ( base_url('news/create') )

So.. what's the way to go with this issue?

正如我所说,一个干净简单的解决方案:

if($this->uri->uri_string() == 'news/create'){ ...

Try this..

<ul class="nav">
    <li class="<?php if(! $this->uri->segment(1) || $this->uri->segment(1) == 'welcome') echo 'active'; ?>">
         <a href="<?php echo base_url() ?>">Home</a>
    </li>
    <li class="<?php if($this->uri->segment(1) == 'news' && $this->uri->segment(2) == 'create' ) echo 'active'; ?>">
        <a href="<?php echo base_url('news/create/') ?>">+ New</a>
    </li>
</ul>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM