简体   繁体   中英

PHP don't repeat code ( don't repeat yourself )

i am using codeigniter (template) library in codeigniter. I always repeat template library code in every function. will you help me to reduce the code.

public function index(){
    //Load View in template file
        $this->template->title('Contacts')
        ->set_layout($this->admin_home_layout)
        ->set_partial('header', 'admin/admin_share/admin_header')
        ->set_partial('sidebar', 'admin/admin_share/admin_sidebar')
        ->set_partial('footer', 'admin/admin_share/admin_footer')
        ->set_partial('hidenbar', 'admin/admin_share/admin_hidenbar')        
        ->build('admin/admin_home');
    }

These 4 lines i am using in every function. how i can use one time in one controller and how to get in function.

->set_partial('header', 'admin/admin_share/admin_header')
    ->set_partial('sidebar', 'admin/admin_share/admin_sidebar')
    ->set_partial('footer', 'admin/admin_share/admin_footer')
    ->set_partial('hidenbar', 'admin/admin_share/admin_hidenbar')

you can set them in the constructor:

public function __construct() {

    parent::__construct();

    //Do your stuf here

}

Or you could create a function for it:

function set_partials() {
    $this->template->set_partial('header', 'admin/admin_share/admin_header')
    ->set_partial('sidebar', 'admin/admin_share/admin_sidebar')
    ->set_partial('footer', 'admin/admin_share/admin_footer')
    ->set_partial('hidenbar', 'admin/admin_share/admin_hidenbar')
}

In this second case, you're index would look like this:

public function index(){
    //Load View in template file
    $this->template->title('Contacts')
    ->set_layout($this->admin_home_layout);

    $this->set_partials();

    $this->template->build('admin/admin_home');
}

i suggest a different logic, just a custom array for your partials like this :

protected $partials = [
    'header' => 'admin/admin_share/admin_header',
    'sidebar' => 'admin/admin_share/admin_sidebar',
    'footer' => 'admin/admin_share/admin_footer',
    'hidenbar' => 'admin/admin_share/admin_hidenbar'
];

and use this function to build layout

public function build_layout()
{
    $this->template->title('Contacts')->set_layout($this->admin_home_layout);
        foreach($this->partials as $k => $partial){
            $this->template->set_partial($k,$partial);
    }
    $this->template->build('admin/admin_home');

}

This is how I do it ( applicable for any framework in PHP that has similar way of rendering views):

Suppose that the controllers look like this:

<?php 
class AdminController extends BaseController{
    public function viewData($id){
        // do something
        $this->render('something static 1');
        $this->render('customview');
        $this->render('something static 2');
        $this->render('something static 3');
    }

}
?>

What I would do is create a base controller to do this rendering tasks ( and more repetitive stuffs):

<?php 

class MyBaseController extends BaseController{

    protected function renderAll($param){
        if(empty($param)){
            return false;
        }
        $this->render('something static 1');
        $this->render($param);
        $this->render('something static 2');
        $this->render('something static 3');
        return true;
    }
}

?>

Once this is done, I will simply inherit the newly created controller in all my controllers, such as:

<?php 
class AdminController extends MyBaseController{
    public function viewData($id){
        // do something
        $this->renderAll('customview');
    }

}
?>

You can re-use this in any controller action you want to, just inherit the custom base controller class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM