CodeIgniter nested controllers?

限于喜欢 提交于 2019-12-11 11:43:34

问题


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

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.

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

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?


回答1:


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

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

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

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.

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>

Hope this helps!



来源:https://stackoverflow.com/questions/6683126/codeigniter-nested-controllers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!