Code Igniter:控制器或视图中的导航逻辑?

发布于 2024-11-05 12:23:51 字数 989 浏览 1 评论 0原文

我有一个名为“sub_nav”的视图,它当前保存每个部分的链接的多维数组。该视图查看控制器名称以获取当前部分,然后循环访问适当的数组集并输出链接。

可行,但感觉也许我应该为导航创建一个控制器?并使 sub_nav 更简单,但仅输出....?有人可以建议吗?

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

`

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.

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>

`

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠檬 2024-11-12 12:23:51

如果内容是静态数组,我会将其放入:

  • 视图文件。控制器不是唯一可以加载视图的地方,调用没有任何问题来自另一个视图文件(您的模板)中的 $this->load->view() 。只需将数组和视图逻辑存储在那里即可。
  • 配置文件。可能听起来很奇怪,但它是存放此类静态数据的完美位置。这样您就不必在每次 load->view() 调用中不断加载数据。只需将配置文件加载到 MY_Controller 或其他东西中,您就可以在任何地方访问它。然后,您可以编写一个导航视图或库,它将输出您发送给它的任何导航数组(换句话说,不要在此处执行任何 html - 只是数据,然后用于视图中的配置项)。我说使用基本控制器是因为可能不需要自动加载这些数据,例如 AJAX 请求。你并不总是需要它。

它绝对不属于控制器,控制器更多地用于处理请求和数据。图书馆是更有可能的候选者。如果内容是动态的(例如来自数据库),那么您肯定想使用模型或库,但在这种情况下,我更喜欢视图文件。除了视图之外,您不太可能在其他任何地方使用导航数组数据。

无论如何,HTML 只要有可能就属于视图。如果您仅在视图中使用导航数组,仅在一个视图中使用,并且它不是动态的,只需将其存储在视图文件中即可。模型和库应该是可重用的,在那里存储静态数据(您可能需要经常更新)对我来说没有意义,但我愿意听取反对者的意见。

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

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

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.

空心↖ 2024-11-12 12:23:51

根据导航数据的来源,我可能会给它一个自己的模型(例如,如果它来自数据库)。至于选择要显示的导航的逻辑,我会将该逻辑放在一个公共基本控制器中,要么在所有页面加载上运行它,要么将其代码放在基本控制器上的方法中,以便那些需要它的页面可以使用它。

如果您的控制器使用类似 $this->data 的内容来保存要发送到视图的数据,那么基本控制器逻辑也可以将导航数据传递到视图。

Depending where the data for the navigation comes from, I might give it a model of its own (if it comes from a database for example). As for the logic for choosing navigation to display, I would put that logic in a common base controller, either running it on all page loads or placing code for it in a method on the base controller, so those pages that need it can use it.

If your controllers use something like $this->data to hold the data to send to the view, then the base controller logic could pass the navigation data to the view too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文