Welcome Guest, Not a member yet? Register   Sign In
Don't Repeat Yourself in CI doesn't exist
#11

[eluser]Colin Williams[/eluser]
Just wanted to point a few specific things out.
#12

[eluser]obiron2[/eluser]
@gh0st

In your example of the car, to produce this as DRY you would store a 'model' of the car properties and thier database locations in a model file.

Pull the car 'model' and use the fields to carry out the select and data return in your DB query and again to populate the fields in the view.

This way, when you change the properties of the car, you only have to change the 'model' and the DB query and view will change automatically.

Of course, you would still need to add the relevant fields to the relevant tables.

This technique is handy for building generic forms - I am working on one at the moment, if I ever get a chance to finish it, I will publlish it in the wiki

DRY is possible with CI, but somtimes you need to make one more lateral thought step.


obiron
#13

[eluser]got 2 doodle[/eluser]
There has to be limits on DRY but you can save a lot of code by capturing those things that you always seem to be doing in each controller by creating an extension of the controller class and put stuff in there that is common.

Here's a snippet from one of my controllers.
Code:
/*******************************************************************************/    
    function welcome()
/*******************************************************************************/    
    
    {
$this->load_views($this->get_pagedata('lmf_home'));

    }
/*******************************************************************************/    
    function explain()
/*******************************************************************************/    
    
    {
$this->load_views($this->get_pagedata('lmf_explain'));

    }
/*******************************************************************************/    
    function products()
/*******************************************************************************/    
    
    {
$this->load_views($this->get_pagedata('lmf_products'),'products');

    }

Each webpage has a unique name which is passed to the controller, this works because the pages are all very similar. The last example 'lmf_products' doesn't use the default view so it passes 'products' to specify a view format. So not wanting to bore you, I think I have definitely cut down on the repetion.

Just as a matter of coherency here is the code in the parent controller.
Code:
/*-----------------------------------------------------------------------*/
/* get data  */      
function get_pagedata($page_name,$menu = false) {
/*-----------------------------------------------------------------------*/

if($menu) {    $active = $menu;
                } else {
            $active = $page_name;
            }
/* default location is Limstone Mtn Farm */        
$business='lmf';    
$business=$this->uri->segment(1);    
    if($business == 'shf') {
            $mode='right';
                } else {
            $mode='left';
            }
        $data['pagemode']=$mode;                        
        $data['icon_pic']="icon_$mode.jpg";                            
        $this->load->model('images');
        $this->load->model('pagedata');
        $this->load->model('sitedata');
        $this->load->model('product_data');
        $data['page_name']= $page_name;
        $data['products']=$this->product_data->get_products_by_business($business);
        $data['page_data'] = $this->pagedata->get_pagedata_by_page_name($page_name);
        $data['keyphrase'] = $this->pagedata->get_keyphrase_by_page_name($page_name);
        $data['description'] = $this->pagedata->get_description_by_page_name($page_name);
        $data['business'] = $this->pagedata->get_business_by_page_name($active);
        $data['menu'] = $this->pagedata->get_menu_by_page_name($active);
        $data['image_data'] = $this->pagedata->get_images_by_page_name($page_name);
        $data['site_url'] = $this->config->item('site_url');
        $data['base_url'] = $this->config->item('base_url');
        $data['css'] = "site";
        
        // initialize some containers
    $cc_main = "";
    $cc_news = "";
    $cc_tag = "";
if(isset($data['page_data'])) {  
        foreach ($data['page_data'] as $segment) {
            
            if ($segment ['location'] == 'cc_main'){
                    $cc_main .= $segment ['data'];
                }
            if ($segment ['location'] == 'cc_news'){
                    $cc_news .= $segment ['data'];
                }
            if ($segment ['location'] == 'cc_tag'){
                    $cc_tag .= $segment ['data'];
                }    
        }
        }
        $data['cc_main']=$cc_main;
        $data['cc_news']=$cc_news;
        $data['cc_tag']=$cc_tag;
$data = array_merge($data,$this->sitedata->get_all_site_keys());
return $data;
  }
/*-----------------------------------------------------------------------*/
/* get views  */      
function load_views($data,$type = 'standard') {
/*-----------------------------------------------------------------------*/

switch ($type) {
default:
case 'standard':
$output = $this->load->view('header',$data,true);
$output .= $this->load->view('l_column',"",true);
$output .= $this->load->view('content',"",true);
    if($this->test) $output .= $this->load->view('helper',"",true);
$output .= $this->load->view('footer',"",true);
break;
case 'products':
$output = $this->load->view('header',$data,true);
$output .= $this->load->view('l_column',"",true);
$output .= $this->load->view('content_products',"",true);
    if($this->test) $output .= $this->load->view('helper',"",true);
$output .= $this->load->view('footer',"",true);
break;
case 'product':
$output = $this->load->view('header',$data,true);
$output .= $this->load->view('l_column',"",true);
$output .= $this->load->view('content_product',"",true);
    if($this->test) $output .= $this->load->view('helper',"",true);
$output .= $this->load->view('footer',"",true);
break;
} // end switch
/* Done loading views */        
        $this->output->set_output($output);
  }  
/*-----------------------------------------------------------------------*/
#14

[eluser]gh0st[/eluser]
I think I learnt a lot with this thread. Perhaps my thoughts of what DRY was/is, is probably off-base.

Thanks
#15

[eluser]got 2 doodle[/eluser]
Maybe you had it mixed up with DRYE

(Dont Repeat Yourself Ever)

:coolsmile:

I have learned more than I could tell from this forum, play nice and these guys are great and very generous with their knowledge.

doodle
#16

[eluser]winter[/eluser]
[quote author="obiron2" date="1235504485"]@gh0st

In your example of the car, to produce this as DRY you would store a 'model' of the car properties and thier database locations in a model file.

Pull the car 'model' and use the fields to carry out the select and data return in your DB query and again to populate the fields in the view.

This way, when you change the properties of the car, you only have to change the 'model' and the DB query and view will change automatically.

Of course, you would still need to add the relevant fields to the relevant tables.

This technique is handy for building generic forms - I am working on one at the moment, if I ever get a chance to finish it, I will publlish it in the wiki

DRY is possible with CI, but somtimes you need to make one more lateral thought step.

obiron[/quote]

Sorry to butt in here, but that sounds really cool. If anyone has some working code examples or tutorials, I'd really enjoy seeing them.

Cheers!




Theme © iAndrew 2016 - Forum software by © MyBB