【问题标题】:CodeIgniter: How to match current_url against views url (to construct a nav)CodeIgniter:如何将 current_url 与视图 url 匹配(构建导航)
【发布时间】:2013-05-02 19:05:45
【问题描述】:

为了将活动类应用于导航菜单中的当前项目。我需要检查它的链接是否等于 URL

我正在尝试这样:

<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 似乎在家里工作得很好

但在新闻/创造中永远不会平等......

它将 news/create (uri_string(current_url()) 与 /news/create (base_url('news/create')) 进行比较

那么.. 解决这个问题的方法是什么?

【问题讨论】:

  • 为什么不直接做if($this-&gt;uri-&gt;uri_string() == 'news/create')
  • 对我来说看起来更干净!将其发布为答案
  • 刚刚发布了答案:)

标签: php codeigniter url base-url


【解决方案1】:

试试这个..

<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>

【讨论】:

  • 这很简单..这才是最重要的 ;)
  • 我同意他/她的看法。你只需要知道调用了哪个控制器和动作!
  • 我在上面的建议,if($this-&gt;uri-&gt;uri_string() == 'news/create') 稍微不那么混乱 :)
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-09
    • 2020-12-13
    • 2014-09-10
    • 2018-08-27
    • 2014-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多