开发者

CodeIgniter nested controllers?

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?


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜