TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-21-2009, 08:07 PM   #1 (permalink)
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
Old 12-24-2009, 06:52 AM   #2 (permalink)
The Contributor
 
Join Date: Feb 2007
Posts: 64
Thanks: 9
Killswitch is on a distinguished road
Default

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.
Killswitch is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Prettifying Pasted Code on TalkPHP Wildhoney The Lounge 15 01-04-2013 02:46 AM
Tips to Improve Your Coding and Projects Village Idiot Tips & Tricks 45 12-03-2012 07:46 AM
Writing Clean Code Village Idiot Tips & Tricks 10 06-25-2012 12:35 PM
Match code in link with code with one in file and extract line Peuplarchie General 0 10-23-2009 02:38 PM
Game: Let's Develop Something Crazy! Wildhoney The Lounge 25 05-23-2009 09:18 PM


All times are GMT. The time now is 06:25 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design