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.
#2

[eluser]Devon Lambert[/eluser]
[quote author="tokyo" date="1268735694"]Hi,
Make use of functions , i think thats the better way to reuse any code.
u have to only work on parameter passing & receiving[/quote]

Hi Tokyo,

That's great advice but could you elaborate on it. I understand that I will need to make use of functions/methods of some kind but I'm unaware of how to execute this in regards to the specific code mentioned above.

Thanks again for the fast response. :-)
#3

[eluser]umefarooq[/eluser]
Hi you can use _remap for reducibility here is little example of controller and view which you can use

Code:
controller

class example extends Controller{
  // your database columns or fields
  $cols = array('username'=>array('rule'=>'trim|required|xss_clean'),'password'=>array('rule'=>'trim|required|xss_clean'));
  function example(){
    parent::Controller();
  }
  
  function _remap($task){
   switch($task){
     case 'add':
     case 'edit':
      $this->form();
      break;
      default:
      $this->view();
      break;
   }
  }
  
  function form(){
     // to get id set you segment  i set 3 for example you have to set it later and default is 0
     $id = $this->uri->segment(3,0);
    
     $this->load->library('form_validation');
    
     // it will set rules for your fields
     foreach($this->cols as $key => $v){
      $this->form_validation->set_rules($key,$key,$v['rule']);
     }
    
     if($this->form_validation->run() == false){
        $row = $this->db->get_where('yourtable',array('id'=>$id))->row();
        //sending row if there is data while edit
        $data = $this->create_form($row);
        //for hidden filed for your form
        $data['id'] = array('id'=>$id)
        $this->load->view('form',$data);
     }
     else{
       $this->save();
     }
    
  }
  
  function save(){
  
    $data['id'] = $this->input->get_post('id');
    foreach($this->cols as $col => $v){
      $data[$col] = $this->input->get_post($col);
    }
    
    if($data['id']){
      $this->db->update('yourtable',$data,array('id'=>$data['id']))
    }
    else{
      $this->db->insert('yourtable',$data);
    }
    redirect('anywhere');
  }
  
  function creat_form($row){
    $fields = array();
    foreach($this->cols as $col=>$v){
      $fields[$col] = array('name'=>$col,'id'=>$col,'value'=>set_value($col,is_object($row)?$row->$col:''));
    }
    return $fields;
  }
    
}

form view

   echo form_open($this->uri->uri_string());
   echo form_input($username);
   echo form_password($password);
   echo from_hidden($id);
   echo form_submit();
#4

[eluser]Devon Lambert[/eluser]
Whoops,

form view... nice. :-)
#5

[eluser]umefarooq[/eluser]
i hope code working fine with you, if you have any problem just post it here.
#6

[eluser]Mareshal[/eluser]
What does _remap() function? because is just defined. Is an internal Codeigniter function?
#7

[eluser]umefarooq[/eluser]
please read this user guide

http://ellislab.com/codeigniter/user-gui...#remapping
#8

[eluser]Mareshal[/eluser]
Thanks, I think I missed that part of CI user guide
#9

[eluser]Devon Lambert[/eluser]
Hey umefarooq,

Again, thanks for being so helpful. Your code does look great but it still doesn't necessarily reduce the amount of code I have to write for all of the Admin Controllers?

i.e. There is still no way to load the necessary model that must match up to each specific controller function.

For instance, the code I presented above is specifically for adding, and editing, new sites within my CMS. However, I repeat much this same process for adding, and editing, themes within my site. I also do the same thing for adding, and editing, pages within my site.

Does this make more sense. If I'm way off then please let me know but I definitely assumed there was some easy way to widdle away at this code and allow me to say drop in the MODULE that I am working on, on the fly, and have it automagically speak to it's respective model?
#10

[eluser]Mareshal[/eluser]
Devon, You can't reduce it too much. It's like you would want to buy a brand new car, with $10 and you still want it cheaper




Theme © iAndrew 2016 - Forum software by © MyBB