Welcome Guest, Not a member yet? Register   Sign In
How to simplify a controller?
#1

[eluser]Juan Velandia[/eluser]
Hello Everyone, I'm looking for a way to avoid repeat all the repeated lines in this controller, Any Ideas?

Code:
function section($section)
    {
        $data['meta_title'] = $query_row->meta_title;
        $data['meta_description'] = $query_row->meta_description;
        $data['meta_keywords'] = $query_row->meta_keywords;
        $data['item_placeholder_1'] = $this->item_placeholder_model->show_item_placeholder_1();
        $data['product_placeholder'] = $this->item_placeholder_model->show_product_placeholder();
        $data['section_articles'] = $this->section_content_model->get_section_articles();
        $data['section_content'] = $this->section_content_model->get_section_content();
        $data['article_header'] = $this->article_content_model->get_article_header($section);

        $partials = array('top_logo'=>'view_top_logo','top_menu'=>'view_top_menu', 'top_banner'=> 'view_top_banner','mid_banner'=> 'view_mid_banner' ,'main_container'=> 'view_main_section','left_menu'=> 'view_left_menu','credit_nemo'=> 'view_credit_nemo', 'meta_head'=> 'view_meta_head');
        $this->template->load('master_section', $partials, $data);
    }

    function product($section)
    {
        $data['meta_title'] = $query_row->meta_title;
        $data['meta_description'] = $query_row->meta_description;
        $data['meta_keywords'] = $query_row->meta_keywords;
        $data['item_placeholder_1'] = $this->item_placeholder_model->show_item_placeholder_1();
        $data['product_placeholder'] = $this->item_placeholder_model->show_product_placeholder();
        $data['section_articles'] = $this->section_content_model->get_section_articles();
        $data['section_content'] = $this->section_content_model->get_section_content();
        $data['article_header'] = $this->article_content_model->get_article_header($section);
        $data['product_content'] = $this->product_content_model->get_product_content();
        $data['product_component'] = $this->product_content_model->get_product_component();
        
        $partials = array('top_logo'=>'view_top_logo','top_menu'=>'view_top_menu', 'top_banner'=> 'view_top_banner','main_container'=> 'view_main_product','left_menu'=> 'view_left_menu','credit_nemo'=> 'view_credit_nemo', 'meta_head'=> 'view_meta_head');
        $this->template->load('master_section', $partials, $data);
    }


    function article()

{
        $data['meta_title'] = $query_row->meta_title;
        $data['meta_description'] = $query_row->meta_description;
        $data['meta_keywords'] = $query_row->meta_keywords;
        $data['item_placeholder_1'] = $this->item_placeholder_model->show_item_placeholder_1();
        $data['product_placeholder'] = $this->item_placeholder_model->show_product_placeholder();
        $data['section_content'] = $this->section_content_model->get_section_content();
        $data['section_articles'] = $this->section_content_model->get_section_articles();
        $data['article_content'] = $this->article_content_model->get_article_content();

        $partials = array('top_logo'=>'view_top_logo','top_menu'=>'view_top_menu','top_banner'=> 'view_top_banner','main_container'=> 'view_main_article','left_menu'=> 'view_left_menu','credit_nemo'=> 'view_credit_nemo','meta_head'=> 'view_meta_head', 'contact_container'=> 'view_contact_form');
        $this->template->load('master_section', $partials, $data);

    }

Thanks a lot!
#2

[eluser]smilie[/eluser]
Sure, easy...

Code:
function show($what='',$section='')
{
        # This is always same
        $data['meta_title'] = $query_row->meta_title;
        $data['meta_description'] = $query_row->meta_description;
        $data['meta_keywords'] = $query_row->meta_keywords;
        $data['item_placeholder_1'] = $this->item_placeholder_model->show_item_placeholder_1();
        $data['product_placeholder'] = $this->item_placeholder_model->show_product_placeholder();
        $data['section_articles'] = $this->section_content_model->get_section_articles();
        $data['section_content'] = $this->section_content_model->get_section_content();
        $data['article_header'] = $this->article_content_model->get_article_header(($section ? $section : ''));

        # This is extra, only in case type = product
        ($what=='product' ? $data['product_content'] = $this->product_content_model->get_product_content() : '');
        ($what=='product' ? $data['product_component'] = $this->product_content_model->get_product_component() : '');
}

Maybe I missed something, double check it.

Idea is, you always call same controller (show) and tell it $what to show and in case $section is present to use it.

Cheers,
Smilie
#3

[eluser]Nick Roper[/eluser]
Or put the code that is always implemented in the constructor
#4

[eluser]techgnome[/eluser]
Or (yet one more option) ... if the code is repeated in other controllers, put it into a base controller (ala "MY_Controller") then extend that class (instead of extending Controller).

If it's not repeater in other controllers, then I'd do all the work in the constructor.
something like this:
Code:
Class Sometest Extends Controller
{
   var $data = array(); // initialize the data array to hold the data
  
   function Sometest()
   {
        $this->data['meta_title'] = $query_row->meta_title;
        $this->data['meta_description'] = $query_row->meta_description;
        $this->data['meta_keywords'] = $query_row->meta_keywords;
        $this->data['item_placeholder_1'] = $this->item_placeholder_model->show_item_placeholder_1();
        $this->data['product_placeholder'] = $this->item_placeholder_model->show_product_placeholder();
        $this->data['section_articles'] = $this->section_content_model->get_section_articles();
        $this->data['section_content'] = $this->section_content_model->get_section_content();
        $this->data['article_header'] = $this->article_content_model->get_article_header(($section ? $section : ''));
   }

function section($section)
    {
        $partials = array('top_logo'=>'view_top_logo','top_menu'=>'view_top_menu', 'top_banner'=> 'view_top_banner','mid_banner'=> 'view_mid_banner' ,'main_container'=> 'view_main_section','left_menu'=> 'view_left_menu','credit_nemo'=> 'view_credit_nemo', 'meta_head'=> 'view_meta_head');
        $this->template->load('master_section', $partials, $this->data);
    }

-tg
#5

[eluser]Juan Velandia[/eluser]
Wow, Lots of knowledge! I´ll work on the three options! I really appreciate it!. Thanks a lot!




Theme © iAndrew 2016 - Forum software by © MyBB