CodeIgniter - Features Available in Multiple Controllers

I am relatively new to CodeIgniter, and so far my project has been completely created using controllers and views. However, as it becomes more complex, I found that there are certain functions that I copied to several controllers - this is hardly ideal, since editing requires remembering and editing all the others.

There are many CI features that I don’t know anything about - models, helpers, the "Controller" extension, etc. etc. Where should I look to accomplish the above? I believe that I could also import() block of code directly, although it seems to me that this is not a “CodeIgniter path”.

+4
source share
3 answers

Put all of your "service" functions in the "assistant manager" and get access to it.

http://codeigniter.com/user_guide/general/helpers.html

+4
source

Or create a base controller and remove other controllers from it.

I am sure Phil Sturgeon has a guide on this: http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY

+2
source

base_controller.php

 <?php class Base_Controller extends CI_Controller { function __construct() { parent::__construct(); } function base_function(){ } } ?> 

other_controller.php

 <?php require_once('base_controller.php'); class Other_Controller extends Base_Controller{ function __construct() { parent::__construct(); } function index() { $this->base_function(); } } ?> 
+2
source

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


All Articles