Controller Design - multi subview - trying to add one master Controller Design - multi subview - trying to add one master codeigniter codeigniter

Controller Design - multi subview - trying to add one master


ok - preface this by disclaimer that coding style is personal, and some people will not agree at all with what i'm going to suggest.

my impression of what you are doing - is that you are trying to do everything in either the controller or the view. right here this code in your controller

 $data['main_content'] = 'x/y';        $data['widget'] = 'x/widget';        $data['heading'] = "";        $data['activeTab'] = 'pzlec';        $data['activeTabSub'] = '2';        $data['strona'] = 'text';        $data['dzial'] = 'text';

this is way too specific. a controller should be a high level boss - that you do not bother with the low level details. "Get me this from the database - did it come back? if yes then do this. if you not then do that."

put another way - the low level details are going to change constantly. you have to plan for those changes. and you do not want to change your controllers constantly because any error will cause your application to go down.

so thats what Models are for. again people will argue with this and say no it should be Libraries or Helpers or whatever. the point is that you want to isolate these low level details and you want to put them in a place where it makes sense. so six months from now you can look at the file names and have a decent idea where you need to go to make updates. its better to have 10 models with specific names and specific tasks, then a few super models that are trying to do too much.

and speaking of trying to do too much - refactor those controller methods so that they are not trying to do so many different things. for example why would you be pulling in a bunch of data if you are going to show a 404 because $seg is not there? checking if $seg is valid should be the first task. if its valid then go to the next method. and if its not valid then don't show a 404 - show a polite page that is specific to the error. then when it happens you have a chance to figure out what the issue was.

finally i would suggest - make your layout template completely neutral - in other words no html code. it just calls other views. again what does that do? it pushes the low level details - like html and css layout code - to their own files. the layout template just calls the layout views. so then when you need to make changes, its clear where to go and if you make an error you don't suddenly bring your template down - you have confined it to a view file, which will be much easier to deal with.


if your tab menu is working you could buffer all your views into one.I assume you are using bootstrap from your sample code.Here is how they suggest you write your markup for the tabs. http://getbootstrap.com/javascript/#tabs-examples Some javascript configuration may be required to get them working, so you will need to read the docs carefully. I'm not a bootstrap guy myself, so I can offer little help with that.

<ul class="nav nav-tabs" role='tablist'>  <li role="presentation" class='active'>      <a href='#widget1' data-toggle="tab">widget1</a>  </li>  <li role="presentation">      <a href='#widget2' data-toggle="tab">widget2</a>  </li></ul>

<section class='tab-content'>   <div class='tab-pane active' id='widget1'>       <?php $this->load->view($widget1); ?>   </div>   <div class='tab-pane' id='widget2'>       <?php $this->load->view($widget2); ?>   </div></section>

To build a master template to wrap around all your views, you would want to extend the CI_Controller class and simply set a property called template, that will point to your template

class MY_Controller extends CI_Controller{    public $template;    public function __construct(){        parent::__construct();        // set the template in the constructor         // as this is where you should assign variables        // If you had an admin controller, you would create an admin controller        // extending this one, and override the $template variable.        $this->template = 'template/index' // views/template/index.php    }}

views/template/index.phpThis is your master view we created by extending CI_Controller

<html>  <head></head>  <body>    <?php      // load "view" variable ANY controller sends us      $this->load->view($view);    ?>  </body></html>

class Controller extends MY_Controller //extending the MY_Controller{   public function index()   {       $data = array('username'=>'stackoverflow');       return $this->load->view($this->template, array(           'view' => 'your_main_view_with_tab_menu',           // the next two views are buffered as a string           // so you can easily inject them into your main tab menu view           // by setting the third paramter as true           'widget1' => $this->load->view('widget1_view', array('data' => $data), true),           'widget2' => $this->load->view('widget2_view', array('data' => $data), true)       ));   }}