使用CodeIgniter高效网站导航 [英] Efficient Website Navigation with CodeIgniter

查看:134
本文介绍了使用CodeIgniter高效网站导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个网站上工作,导航中最多可以有3个层级。







  • b
    $ b

    在过去,当我使用PHP滚动我自己,我会创建一个文件navigation.php持有一个类和数组的所有部分和子部分和几个函数输出导航。我会在网站的每一个页面上设置变量(current_section ='',current_sub_section =''),以便导航功能知道要突出显示的内容。



    在CodeIgniter中,我想知道这是否仍然是一个好的方法使用?

    解决方案

    我将假设你的主要nav部分几乎直接映射到你的控制器,例如



    首页|新闻|活动



    在您的顶部栏中,映射到:




    • / home

    • / news

    • / events


    $ b b

    如果是这样,您已经有一个简单的选择导航数组的方法。



    您可以将nav项目 - 控制器构造器&



    示例:

      class HomeController extends CI_Controller 
    {
    private $ nav;
    public function __construct()
    {
    parent :: __ construct();

    $ this-> nav = array(
    array('Browse',site_url('news / browse')),
    array('Edit',site_url新闻/编辑'))
    );

    $ this-> load-> vars(array('NavigationArray'=> $ this-> nav));
    }

    // ...

    }


    b $ b

    现在你所做的是在所有视图中自动注册一个变量 $ NavigationArray ,它包含一个显示名称 - / strong>对。



    然后,您可以加载基本导航视图,从该变量构建您的subnav(因为它随处可用)。

     < ;? foreach($ NavigationArray as $ entry):?> 
    < a href =<?= $ entry [1];?>><?= $ entry [0];?>< / a>
    < ;? endforeach; >

    下面你可以找到一个sub-nav数组的存在您的控制器或任何(您所谈论的第三个可选导航)

     <? if(exists($ SubNavigationArray)):?> 
    < ;? foreach($ SubNavigationArray as $ entry):?>
    < a href =<?= $ entry [1];?>><?= $ entry [0];?>< / a>
    < ;? endforeach; >
    < ;?万一; >

    请记住,此示例非常基本,但这通常是我们如何传递数据,不想让你把全局变量放在任何地方,并尝试排队它们。只需加载变量到视图引擎,并且当你去渲染你的视图/子视图时,它们将可用。



    这样控制器 / strong>显示哪些导航项目。



    还请注意:



    相信它们将存在于您的视图的范围内。

      $ this-> load-& array('NavigationArray'=> $ this-> nav)); 

    希望这有帮助。


    I'm working on a site where there can be up to 3 levels of hierarchy in the navigation.

    • main sections in the top header
    • sub-navigation on the left
    • optional sub-sub navigation below that

    In the past, when I've rolled my own with PHP, I would create a file navigation.php holding a class and arrays for all the sections and sub sections and a couple of functions to output the navigation. I'd set variables on every page in the site (current_section ='', current_sub_section='') so the navigation function would know what to highlight.

    In CodeIgniter, I'm wondering if this is still a good approach to use?

    解决方案

    I'm going to assume that your main nav sections almost directly map to your controllers, ie.

    Home | News | Events

    In your top bar, maps to:

    • /home
    • /news
    • /events

    If this is the case, you already have a simple way of selecting your nav array.

    You can put an array of nav item-link pairs in your controller constructor & pass them along to a sub-view in your output.

    Example:

    class HomeController extends CI_Controller 
    {
      private $nav; 
      public function __construct() 
      {
        parent::__construct();
    
        $this->nav = array(
           array('Browse', site_url('news/browse')),
           array('Edit', site_url('news/edit'))
        );
    
        $this->load->vars(array('NavigationArray' => $this->nav));
      }
    
      // ...
    
    }
    

    Now what you've done is auto-registered a variable in all your views $NavigationArray that contains an array of Display Name - Link pairs.

    You can then load a basic Navigation View that builds up your subnav from that variable (as it's availiable everywhere).

    <? foreach($NavigationArray as $entry): ?>
       <a href="<?=$entry[1];?>"><?=$entry[0];?></a>
    <? endforeach; ?>
    

    And below that you can look for the existance of a sub-nav array that you could optionally set in your controller, or whatever (the third optional nav you talked about)

    <? if(exists($SubNavigationArray)): ?>
      <? foreach($SubNavigationArray as $entry): ?>
         <a href="<?=$entry[1];?>"><?=$entry[0];?></a>
      <? endforeach; ?>
    <? endif; ?>
    

    Please remember, this example is very basic, but this is generally how we pass data around, I wouldn't want you to put global variables anywhere and try to queue off them. Simply "load" variables into the view engine, and they'll be availiable when you go to render your views/subviews.

    This way the controller controls what nav items get displayed.

    Also note:

    You can pass the variables explicitly instead of trusting they will exist in the scope of your view.

    $this->load->view('myview', array('NavigationArray' => $this->nav));
    

    Hope this helps.

    这篇关于使用CodeIgniter高效网站导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆