代码点火器:控制器或视图中的导航逻辑? [英] Code Igniter: This navigation logic in a controller or view?

查看:95
本文介绍了代码点火器:控制器或视图中的导航逻辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图称为sub_nav,它当前拥有每个部分的链接的多维数组。此视图查看控制器名称以获取当前部分,然后循环通过相应的数组集并输出链接。

I have a view called 'sub_nav' and it currently holds a multidimentional array of the links for each section. This view looks at the controller name to get the current section and then loops through the appropriate array set and outputs the links.

工作原理,但感觉像我可能应该创建一个控制器只为nav?并使sub_nav更简单,但只输出....?

that works works but it feels like maybe I should create a controller just for the nav? and make sub_nav simpler but only outputing.... ? can anyone advice?

$controllerName = $this->router->class; 
$methodName = $this->router->method; 

$subLinks['about'] = array( 
                        'introduction'  => 'Introduction',
                        'people'        => 'Our People'
                    );

$subLinks['contact'] = array(   
                        'singapore'     => 'Singapore', 
                        'japan'         => 'Japan'
                    );

?>

<ul>
    <?php foreach($subLinks[$controllerName] as $link=>$linkName){ ?>

    <li <?php if($methodName == $link){ ?>class="on"<? } ?>><a href="<?php   echo base_url(); ?><?php echo $controllerName ?>/<?php echo $link ?>/"><?php echo   $linkName ?></a></li>

    <? } ?>
</ul>

`

推荐答案

如果内容是静态数组,我会把它放在:

If the content is a static array, I would put it in either:


  • strong>一个视图文件。控制器不是唯一可以加载视图的地方,调用 $ this-> load-> view()从另一个视图文件(您的模板)。只需存储数组视图逻辑。

  • 配置文件。可能听起来很奇怪,但它是静态数据这样。这样,您不必在每次load-> view()调用中不断加载数据。只需加载配置文件在 MY_Controller 或者什么,你可以访问它在任何地方。然后你可以编写一个导航视图或库,它会输出你发送给它的任何导航数组(换句话说,不要在这里做任何html - 只是数据,然后用于视图中的配置项)。我说使用基本控制器,因为可能不需要自动加载此数据,对于AJAX请求实例。

  • A view file. Controllers aren't the only place views can be loaded, there's nothing wrong with calling $this->load->view() from within another view file (your template). Just store the array and the view logic there.
  • A config file. May sound strange, but it's a perfect place for static data like this. This way you won't have to constantly load the data in every load->view() call. Just load the config file in MY_Controller or something, and you have access to it anywhere. Then you can write a navigation view or library that will output whatever navigation array you send to it (in other words, don't do any html here - just data, then use for the config item in your view). I say to use a base controller because there's probably no need to autoload this data, for an AJAX request for instance. You won't always need it.

它绝对不属于控制器,更适用于处理请求和数据。图书馆是更可能的候选人。如果内容是动态的(例如从数据库),那么你肯定想使用一个模型或库,但在这种情况下,我更喜欢一个视图文件。除了视图之外,你不可能使用导航数组数据。

It definitely doesn't belong in a Controller, which is more for processing requests and data. A library is a more likely candidate. If the content was dynamic (from the database for example) then you'd definitely want to use a Model or Library, but in this case I would prefer a view file. It's not likely you'll be using the navigation array data anywhere else except your views.

在任何情况下,HTML都属于视图。如果你只在视图中使用导航数组,只能在一个视图中,它不是动态的,只需将其存储在视图文件中。模型和库应该是可重用的,存储静态数据(你可能需要经常更新)对我没有意义,但我愿意听对手。

In any case, HTML belongs in views whenever possible. If you only use the navigation array in views, only in one view, and it's not dynamic, just store it right there in the view file. Models and libraries should be reusable, storing static data there (that you'll probably need to update frequently) makes no sense to me, but I'd be willing to listen to opponents.

这篇关于代码点火器:控制器或视图中的导航逻辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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