View Single Post
Old 12-21-2009, 08:07 PM   #1 (permalink)
xperience
The Wanderer
 
Join Date: Dec 2007
Posts: 18
Thanks: 2
xperience is on a distinguished road
Default Making code more DRY

I am currently working in CodeIgniter. I'm building an Admin for a website and basically doing CRUD over and over. I am attempting to now just build a base class that can take care of most of the behaviors I need and then just override whatever else.


Here is what I attempting for a base class. I'm not quite sure where to go with this at all.


PHP Code:
<?php

class Base Extends Controller {
  
  protected 
$_model;

  public function 
__construct() {
    
parent::__construct();
  }
  
  public function 
add($id NULL) {
    if (
$this->input->post('add')) {
      
      if (
$this->$this->_model->add($data)) {
        
      }
      
    }
  }
  
  public function 
edit() {
    
  }
  
  public function 
delete() {
    
  }
  
  public function 
render($page) {
    
$this->load->view('template/header');
    
$this->load->view($page);
    
$this->load->view('template/footer');
  }
  
  private function 
is_set($data) {
    return (isset(
$data) && !empty($data)) ? '1' '0';
  }

}

?>
Here is an example of what I normally do.

PHP Code:
<?php

class Class_Pages extends Controller {
  
  public function 
__construct() {
    
parent::Controller();
    
$this->load->Model('Class_Pages_model''Pages');
  }
  
  public function 
add($section_id$class_id) {
    
$data['section_id'] = $section_id;
    
$data['class_id']   = $class_id;
    
    if (
$this->input->post('add_page')) {
      
$data = array(
        
'id'          => 'NULL',
        
'section_id'  => $section_id,
        
'title'       => $this->input->post('title'),
        
'slug'        => $this->input->post('slug'),
        
'content'     => $this->input->post('content'),
        
'rank'        => $this->input->post('rank')
      );
    
      if (
$this->Pages->add_page($data)) {
        
$this->session->set_flashdata('message''<div class="greenBox" id="message"> '          
         
'<strong>Sucessfully added Page:</strong> ' $this->input->post('title') . '</div>');
      } else {
        
$this->session->set_flashdata('message''<div class="redBox" id="message"> Oops we were unable to add the Page. Please try again ! </div>');
      }
      
redirect('/classes/manage/' $class_id '/' $section_id);  
    }
    
    
$this->render('add'$data);
  }
  
  public function 
edit($page_id$class_id) {
    
$data['page_id']  = $page_id;
    
$data['page']     = $this->Pages->get_page($page_id);
    
$section_id       $data['page']->section_id;
    
    if (
$this->input->post('edit_page')) {
      
$data = array(
        
'section_id'  => $section_id,
        
'title'       => $this->input->post('title'),
        
'slug'        => $this->input->post('slug'),
        
'content'     => $this->input->post('content'),
        
'rank'        => $this->input->post('rank')
      );
    
      if (
$this->Pages->edit_page($page_id$data)) {
        
$this->session->set_flashdata('message''<div class="greenBox" id="message"> '          
         
'<strong>Sucessfully edited Page:</strong> ' $this->input->post('title') . '</div>');
      } else {
        
$this->session->set_flashdata('message''<div class="redBox" id="message"> Oops we were unable to edit the Page. Please try again ! </div>');
      }
      
redirect('/classes/manage/' $class_id '/' $section_id);
    }

    
$this->render('edit'$data);
  }

  private function 
render($page$data NULL) {
    
$this->load->view('template/header');
    
$this->load->view('pages/' $page$data);
    
$this->load->view('template/footer');
  }

}


?>
Any suggestions to make this code more DRY, so I'm not repeating the basic same functionality over and over would be awesome !
xperience is offline  
Reply With Quote