Codeigniter - makes my controllers drier

In my codeigniter controller function, I use the following code to create my view and insert all the necessary content:

$left_column = $this->load->view('widgets/sub_navigation', $subnav_data, true); $left_column .= $this->load->view('widgets/search_box', '', true); //Set data to be loaded into columns $left_column_base = $this->load->view('widgets/assist_navigation', '', true); $center_column = 'this is center column'; $right_column = $this->load->view('widgets/ask_us_a_question', '', true); $right_column .= $this->load->view('widgets/newsletter', '', true); $right_column .= $this->load->view('widgets/latest_news', '', true); $this->template->inject_partial('left_column', $left_column); //Inject data into the partial columns $this->template->inject_partial('left_column_base', $left_column_base); $this->template->inject_partial('center_column', $center_column); $this->template->inject_partial('right_column', $right_column); $this->template->build('template',$data); 

I am using a three-column layout, and the code above determines what is shown in each column. It works very modularly, which allows me to quickly customize every page.

Is there a way to simplify the above code, possibly using arrays to reduce duplicate code, making things drier?

+6
source share
3 answers

Codeigniter controllers are developed after the script transaction template, he knows that the controller, as a rule, becomes large and "not dry" when the application grows.

To prevent this from happening, you can reimplement the view to handle a two-stage composite view that supports layouts. Take a look at the presentation of the IIRC layout on the codeigniter website.

+1
source

You need to create basic controllers that extend the CI_Controller. Then all your controllers expand the specific base controller that you created, depending on what you need to do in all cases when the controller is called.

In application/core create a file called MY_controller.php (the prefix can be changed in config):

 class MY_Controller extends CI_Controller { function __construct() { parent::__construct(); /* Widgets are only prepared -- they will be fetched and rendered once layout->render is called. This saves the overhead of reading the files on requests where layout isn't rendered. */ $this->layout->prepare_widget( "widgets/navigation", "navigation_widget" ); $this->layout->prepare_widget( "widgets/footer", "footer_widget" ); $this->layout->prepare_widget( "widgets/twitter", "twitter_widget" ); } } class Public_Controller extends MY_Controller { function __construct() { parent::__construct(); } } class Admin_Controller extends MY_Controller { function __construct() { parent::__construct(); if( !$this->user->has_permissions( PERMISSION_ADMIN ) ) { redirect( base_url(), "location", 303 ); die(); } } } class Member_Controller extends MY_Controller { function __construct() { parent::__construct(); if( !$this->user->has_permissions( PERMISSION_REGISTERED ) ) { redirect( base_url(), "location", 303 ); die(); } } } 

As you can see, all subcontrollers have widgets automatically because they extend either public, admin or member. An additional controller that extends the administrator controller automatically checks permissions, so you no longer need to do this. You can apply this concept to your application.

Sub-controller: (fits into regular application/controllers )

 class Articles extends Member_controller { ... } 

It will automatically ensure that the user is logged in and the widgets are prepared without any action, because the parent parent class has already prepared them. All you need to do in the articles is to call $this->layout->render if the logic needs to render the layout at the end.

+4
source

I wrote a blog post explaining my design philosophy for organizing CodeIgniter controllers to make them drier. I like the index function of my controller to serve as a common entry / exit point, to avoid repeating operations that are common to all controller methods.

http://caseyflynn.com/2011/10/26/codeigniter-php-framework-how-to-organize-controllers-to-achieve-dry-principles/

0
source

Source: https://habr.com/ru/post/902852/


All Articles