Welcome Guest, Not a member yet? Register   Sign In
TRUE contoller CRUD? Another way to not repeat myself?
#1

[eluser]Devon Lambert[/eluser]
While working on the Admin section of my site, I find that I'm constantly writing same chunks of code over and over again in my controllers.

Can someone please help me break this insane cycle? :-)

Example of the code:

Bit of code from admin.php for my site management
Code:
function add($form_submission = false)
    {
        $this->data->themes = $this->themes_m->get_all();

        if ($form_submission == true) {
            
            $this->form_validation->set_rules($this->rules);
            
            if ($this->form_validation->run() == false) {
            
                $this->template->build('admin/add_form', $this->data);
            } else {
                
                if ($this->sites_m->create($_POST) > 0) {
                
                    $this->session->set_flashdata("success", "The site got saved!");
                    
                    redirect('admin/sites');
                    
                } else {
                
                    $this->session->set_flashdata("error", "The site was not saved!");
                    
                    redirect('admin/sites');
                }
            }
            
        } else {
            $this->template->build('admin/add_form', $this->data);            
        }
    }

    function edit($form_submission = 'false', $id = null)
    {
        if ($form_submission === 'true') {

            $this->form_validation->set_rules($this->rules);
            
            if ($this->form_validation->run() == false) {
            
                $this->template->build('admin/edit_form', $this->data);
            } else {
                
                if ($this->sites_m->update($_POST) > 0) {
                
                    $this->session->set_flashdata("success", "Your changes were saved!");
                    
                    redirect('admin/sites');
                    
                } else {

                    $this->session->set_flashdata("error", "Something went wrong. The changes didn't take!");
                    
                    redirect('admin/sites');
                }
            }
            
        } else {
            $this->data->site = $this->sites_m->get($id);
            
            $this->template->build('admin/edit_form', $this->data);
        }
    }

Is there some way for me to make use of this code in a reusable way? Realize that I have tried, and failed, to place this code in both a Parent Controller as well as in a helper.


Messages In This Thread
TRUE contoller CRUD? Another way to not repeat myself? - by El Forum - 03-15-2010, 11:08 PM
TRUE contoller CRUD? Another way to not repeat myself? - by El Forum - 03-15-2010, 11:58 PM
TRUE contoller CRUD? Another way to not repeat myself? - by El Forum - 03-16-2010, 02:54 AM
TRUE contoller CRUD? Another way to not repeat myself? - by El Forum - 03-16-2010, 07:26 PM
TRUE contoller CRUD? Another way to not repeat myself? - by El Forum - 03-17-2010, 12:06 AM
TRUE contoller CRUD? Another way to not repeat myself? - by El Forum - 03-17-2010, 02:11 AM
TRUE contoller CRUD? Another way to not repeat myself? - by El Forum - 03-17-2010, 04:02 AM
TRUE contoller CRUD? Another way to not repeat myself? - by El Forum - 03-17-2010, 04:06 AM
TRUE contoller CRUD? Another way to not repeat myself? - by El Forum - 03-17-2010, 10:23 AM
TRUE contoller CRUD? Another way to not repeat myself? - by El Forum - 03-17-2010, 10:31 AM
TRUE contoller CRUD? Another way to not repeat myself? - by El Forum - 03-17-2010, 10:45 AM
TRUE contoller CRUD? Another way to not repeat myself? - by El Forum - 03-17-2010, 10:49 AM
TRUE contoller CRUD? Another way to not repeat myself? - by El Forum - 03-17-2010, 10:58 AM
TRUE contoller CRUD? Another way to not repeat myself? - by El Forum - 03-17-2010, 11:52 PM
TRUE contoller CRUD? Another way to not repeat myself? - by El Forum - 03-18-2010, 12:26 AM
TRUE contoller CRUD? Another way to not repeat myself? - by El Forum - 03-18-2010, 01:41 AM



Theme © iAndrew 2016 - Forum software by © MyBB