CodeIgniter嵌套控制器? [英] CodeIgniter nested controllers?

查看:122
本文介绍了CodeIgniter嵌套控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是CodeIgniter的新手,希望我的问题能得到一个简单的答案.

I'm new to CodeIgniter and I hope that my question will have a simple answer.

我有一个带有几个菜单项(菜单,菜单和菜单C)的网站. 我已经使用一个主控制器,控制器中的index(),menuA(),menuB()和menuC()对它进行了建模.所调用的函数将设置会话值currentMenu,并包括页眉,X和页脚. X取决于所调用的函数.页眉然后高亮显示所选菜单.

I have a website with a couple of menu items (menuA,menuB and menuC). I have modeled it with one main controller, index(), menuA(), menuB() and menuC() in the controller. The called function sets a session value currentMenu and includes header, X, footer. Where X depends on the function called. The header then high lights the choosen menu.

在menuC内(我的Web应用程序中的帐户设置),我想要一个不同的控制器来控制AccountSettings/NotLoggedIn的子视图.

Within menuC (Account settings in my webapp) I would like to have a different controller that controls subviews of AccountSettings/NotLoggedIn.

从逻辑上讲,我希望menuC()包含页眉和页脚,然后将调用转发给管理登录名或子页面的子控制器.

Logically I would like menuC() to include the header and the footer but then forward the call to a subcontroller that managed login or the sub pages.

我是否错误地使用了框架,或者是否有直接的方法来实现这一目标?

Am I using the framwork wrong or is there a straight forward way to achieve this?

推荐答案

我认为您似乎不了解如何将MVC应用于您的结构.以这种方式描绘它:

I think it sounds like you're not understanding how to apply MVC to your structure. Picture it this way:

控制器代表用户可以与之交互的应用程序的某些方面.例如,我可能有一个items控制器,该控制器允许用户创建,读取,更新或删除items.与items进行交互的所有逻辑均由该控制器处理(这意味着它将调用items模型并呈现必要的视图).

Controllers represent some facet of your application that users can interact with. For example, I could have an items controller that allows users to create, read, update, or delete items. All the logic for interacting with items is handled by that controller (meaning it calls the items model and renders the necessary views).

在您的情况下,听起来好像您正在构建一个pages控制器,该控制器处理显示用户可能调用的特定页面的内容.因此您的控制器可能看起来像这样:

In your case it sounds like you are building a pages controller that handles displaying the content for specific pages a user may call. So your controller could look something like this:

class Page extends CI_Controller {

    public function index()
    {
        // Logic to render home page
    }

        public function about()
    {
        // Logic to render the about page
    }

    //  ... etc ...

当您处理重叠的复杂网站时,

视图可能会有些棘手.我在此过程中发现的最有用的技巧之一是使用模板库来减少冗余.这是我一直使用的: http://williamsconcepts.com/ci /codeigniter/libraries/template/reference.html .使用模板库,您可以轻松定义一个包含页眉和页脚的布局,然后只传递要显示的内容的部分内容.

Views can get a little tricky when you're dealing with complex sites that have overlap. One of the most useful tricks I've discovered along the way is using a emplating library to reduce redundancy. This is the one I use all the time: http://williamsconcepts.com/ci/codeigniter/libraries/template/reference.html . Using the template library you can easily define a layout that includes your header and footer and then just pass in a partial for the content you want to display.

当您要处理菜单之类的逻辑时,您需要做的就是传递带有页面名称的变量,然后执行一些基本的PHP渲染菜单.

When you want to deal with logic in something like a menu, all you need to do is pass in a variable with the page name and then do some basic PHP render the menu.

// Say we pass in a variable called $current to our view
// $current contains the name of the current page
// So say $current = 'About' for this example.


$sitemenu = array(
    array('/', 'Home'),
    array('/about', 'About'),
    array('/help', 'Page 2'),
    array('/contact', 'Page 3')
); ?>
<nav>
  <ul>
  <?php foreach( $sitemenu as $page) { ?>
     <?php if($current == $page[1]) { ?>
         <li class="current"><a href="<?php echo $page[0]; ?>"><?php echo $page[1]; ?></a></li>
     <?php } else { ?>
         <li><a href="<?php echo $page[0]; ?>"><?php echo $page[1]; ?></a></li>
     <?php } ?>
  <?php } ?>
  </ul>
</nav>

希望这会有所帮助!

这篇关于CodeIgniter嵌套控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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