[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);
}
/*-----------------------------------------------------------------------*/