• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
First CI site -> Question regarding controllers for pages

#1
[eluser]rt30000[/eluser]
Hello. I am working on on my first CI (actually my first MVC usage as well) site and I believe I am making good progress. One question I have before proceeding too far, is about controllers/pages. I understand how they work with views, etc. I just question whether I am using them correctly. For example, i have a site with ~5 pages each with many subpages. Is it correct to have 5 controllers (for each section)? My pages are using multiple views, and is not yet hooked up to a db so pardon all the fields. This approach works, just not sure if it is best pracitices. Seems like alot of repetitive code when I get into many subpages. A sample of the products and products/cheese page code (controller)

Code:
<?php

class Products extends Controller {

    function Products()
    {
        parent::Controller();    
    }
    
    function index()
    {
        /* THIS IS OUR PRODUCTS OVERVIEW PAGE */
        /* Load necessary data for template */
        $template['page_title'] = 'Green Field Farms Products | Certified Organic Foods';
        $template['body_class'] = 'cat_Products';
        $template['header_img'] = '';
        $template['header'] = $this->load->view('components/header_view.php', '', true);
        $template['page_headline'] = 'Green Field Farms Products';
        $template['page_headline_img'] = base_url().'public/img/headline-ProductsOverview.gif';
        $template['page_content'] = '<p><a class="edit" href="#">Download our current product list</a> (pdf)</p>
                <div style="border:1px solid #000000; width:240px; padding:10px; float:left; margin-right:15px;"><a href="/index.php/products/cheese">Cheeses</a></div>
                <div style="border:1px solid #000000; width:240px; padding:10px; float:left; margin-right:15px;">Eggs</div>
                <div style="border:1px solid #000000; width:240px; padding:10px; float:left; margin-right:15px; margin-top:15px;">Seasonal Produce</div>
                <div style="border:1px solid #000000; width:240px; padding:10px; float:left; margin-right:15px; margin-top:15px;">Maple Products</div>
                <p style="clear:both;">&nbsp;</p>';
        $footer['footer_content'] = 'Green Field Farms  &bull;  5515 County Road 229  &bull;  Fredericksburg, Ohio 44627  &bull;  Phone: 330.695.2462  &bull;  Fax: 866.281.8052  &bull; Email: <a href="mailto:[email protected]">[email protected]</a>';
        /* Load necessary subcomponent views for this page */
        $template['head'] = $this->load->view('components/head_view.php', '', true);
        $template['navmain'] = $this->load->view('components/navmain_view.php', '', true);
        $template['subcontent']= '';
        $template['footer'] = $this->load->view('components/footer_view.php', $footer, true);
        /* Load master page view and pass along required template parameters ($template) */
        $this->load->view('master_view', $template);  
    }
    
    function cheese()
    {
        /* THIS IS OUR CHEESE OVERVIEW PAGE */
        /* Load necessary data for template */
        $template['page_title'] = 'Cheese Products | Green Field Farms Products | Certified Organic Foods';
        $template['body_class'] = 'cat_Products';
        $template['header_img'] = '';
        $template['header'] = $this->load->view('components/header_view.php', '', true);
        $template['page_headline'] = 'Green Field Farms Organic Cheese Products';
        $template['page_headline_img'] = '';
        $template['page_content'] = '<p>You found the cheese page!!!</p>';
        $navsub['navsub_items'] = array('link1', 'link2', 'link3', 'link4');
        $footer['footer_content'] = 'Green Field Farms  &bull;  5515 County Road 229  &bull;  Fredericksburg, Ohio 44627  &bull;  Phone: 330.695.2462  &bull;  Fax: 866.281.8052  &bull; Email: <a href="mailto:[email protected]">[email protected]</a>';
        /* Load necessary subcomponent views for this page */
        $template['head'] = $this->load->view('components/head_view.php', '', true);
        $template['navmain'] = $this->load->view('components/navmain_view.php', '', true);
        $template['subcontent']= $this->load->view('components/navsub_view.php', $navsub, true);
        $template['footer'] = $this->load->view('components/footer_view.php', $footer, true);
        /* Load master page view and pass along required template parameters ($template) */
        $this->load->view('master_view', $template);
    }
    
}

/* End of file products.php */
/* Location: ./system/application/controllers/products.php */

If I am not explaining this clearly, fire away any questions! Thank you for any help. So far I think I will really benefit from using this framework. Thanks!

#2
[eluser]crumpet[/eluser]
you can call views from within views
so you can have a view file called "loader.php" which calls hte header, and then the nav, and then the content and then the footer etc...
i usually make a function in my controller like this
Code:
function _run($view){
$this->template['module'] = $view;
$this->load->view('loader');
}
Code:
//loader.php
$this->load->view('header');
$this->load->view('nav');
$this->load->view($module);
$this->load->view('footer');

Then the functions in my controller don't call $this->load->view() instead tehy call $this->_run()


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.