TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Making code more DRY (http://www.talkphp.com/advanced-php-programming/5180-making-code-more-dry.html)

xperience 12-21-2009 08:07 PM

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 !

Killswitch 12-24-2009 06:52 AM

There will be a little repitition, it's kinda hard to cut down on without getting extremely complex in your code. I have found however, that by switching to Kohana I have been able to cut back on a lot of extra work with views and a few other things by using their template_controller and all their factory chaining goodness :D

I have a similar app where I had to design an admin page. I didn't write a bunch of the same stuff over and over again, but I did need authentication a lot (which I used Simple_Auth for). I wrote a class to extend the template_controller (for the sake of understanding, since CI doesn't have template_controller, it basically allows you to avoid requiring/using new views for headers, footers, etc) and in the constructor I have a default access level.

I can pass either a required access level (such as administrator, moderator, editor or whatever) or an array of allowed groups. So I call the parent...

Code:

parent::__construct(array('administrator', 'moderator'));
I know you've started with Code Ignitor and know CI (maybe even your client wants to use CI), but look into Kohana.


All times are GMT. The time now is 01:30 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0