Welcome Guest, Not a member yet? Register   Sign In
Same Class, different controllers?
#4

[eluser]kb1ibh[/eluser]
[quote author="GrahamDj28" date="1336082991"]When reading this al I can think of is that this is not the way MVC must be used.

Controllers call functions in libraries, helpers or models.
Helpers, models or libraries don't call functions in controllers.[/quote]

Everything i do is still MVC, its a method that i use to make view management much more modular and simple... i'll create a very very simple template in "/views/controller_templates"

nav363.php:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
?>
<!DOCTYPE html>
&lt;html lang="en"&gt;

&lt;head&gt;
&lt;?=$head?&gt;
&lt;/head&gt;

&lt;body&gt;
<div class="navbar navbar-fixed-top">
  &lt;?=$navbar ?&gt;
</div>
<div class="container">
  <div class="row">
   <div class="span3">
    &lt;?=$leftnav ?&gt;
   </div>
   <div class="span6">
    &lt;?=$content ?&gt;
   </div>
   <div class="span3">
    &lt;?=$rightnav ?&gt;
   </div>
  </div>
  <hr>
  <footer>
   &lt;?=$footer ?&gt;
  </footer>
</div>
&lt;/body&gt;

Then create a controller for that specific layout that assigns default views to those page sections

NAV363_Controller
Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class  NAV363_Controller  extends  MY_Controller  {

    function __construct()  
{
        parent::__construct();
  
$this->data['head'] = &$this->head;
$this->data['navbar'] = &$this->navbar;
        $this->data['leftnav'] = &$this->leftnav;
        $this->data['content'] = &$this->content;
        $this->data['rightnav'] = &$this->rightnav;
        $this->data['footer'] = &$this->footer;
  
        $this->head = $this->load->view('Defaults/head', '', true);
        $this->navbar = $this->load->view('Defaults/navbar', $this->auth, true);
        $this->leftnav = $this->load->view('Defaults/leftnav', '', true);
        $this->content = $this->load->view('Defaults/content', '', true);
$this->rightnav = $this->load->view('Defaults/rightnav', '', true);
        $this->footer = $this->load->view('Defaults/footer', '', true);
}

function output()
{
  $this->load->view('Controller_Templates/nav363', $this->data);
}
}

all of the default views are empty except for Navbar and Footer, because those never change from page to page, and the Head view contains the global CSS and script files...

When i need to add things to a page, i only have to fill in the leftnav, rightnav, and content areas, which is as easy as

Code:
$this->rightnav = $this->load->view('ads/wide_skyscraper', NULL, TRUE);

adding multiple views, or adding to (as opposed to overwriting) the default, you just concatinate

like this:
Code:
$this->head .= $this->load->view('scripts/script_a', '',TRUE);
$this->head .= $this->load->view('scripts/script_b', '',TRUE);
$this->head .= $this->load->view('scripts/script_c', '',TRUE);


then output using $this->output(); and you're done.

makes it easy to divide your site into tiny sections while still making it easy to inject them later on, and you arent wasting space calling the same default views in every controller function, the only problem is that codeigniter by default only allows one custom controller (My_Controller), so you have to make a slight modification by adding the following to the end of config.php:

Code:
function __autoload($class)
{
    if (strpos($class, 'CI_') !== 0)
    {
        @include_once(APPPATH . 'core/' . $class . EXT);
    }
}


Messages In This Thread
Same Class, different controllers? - by El Forum - 05-03-2012, 03:02 PM
Same Class, different controllers? - by El Forum - 05-03-2012, 03:09 PM
Same Class, different controllers? - by El Forum - 05-03-2012, 03:51 PM
Same Class, different controllers? - by El Forum - 05-03-2012, 04:33 PM
Same Class, different controllers? - by El Forum - 05-04-2012, 04:46 AM
Same Class, different controllers? - by El Forum - 05-04-2012, 08:45 AM



Theme © iAndrew 2016 - Forum software by © MyBB