Home >Backend Development >PHP Tutorial >Share a brief summary about the use of php CI framework
I have been using the CI framework frequently recently. This is mainly about the development of the backend. I will focus on sharing a little summary about the Model and Controller layers:
1. When it comes to interactive databases, we first add the database we need to use in database.php. Connection information, as shown in the following example (relevant information needs to be completed):
$db['name']['hostname'] = '';
$db['name']['username'] = '';
$db['name']['password'] = '';
$db['name']['database'] = '';
$db['name']['dbdriver'] = '';
$db['name']['dbprefix'] = '';
$db['name']['pconnect'] = FALSE;
$db['name']['db_debug'] = FALSE;
$db['name']['cache_on'] = FALSE;
$db['name']['cachedir'] = '';
$db['name']['char_set'] = '';
$db['name']['dbcollat'] = '';
$db['name']['swap_pre'] = '';
$db['name']['autoinit'] = TRUE;
$db['name']['stricton'] = FALSE;
2. Then in our Model: $this->db=$this->load->database("name",TRUE) ; This way we can use the database we configured in the Model (multiple databases can be referenced).
can be referenced in function __construct() or in a specific method;
3. Then we can write a function to implement specific sql functions. Here is a relatively simple function connection database template:
function funcname($canshu1,$canshu2)
where game_id = ?
canshu1,
$canshu2 Return true;
} else return false;
}
4. Reference the Model in the Controller, $this->load->model('modelname'); and you can use the methods in the model;
5. In the function of the Controller The function of using model in $return = $this->modelname->funcname($canshu1,$canshu2);
Then continue to complete our functions.
The above is a relatively direct access method. We can combine it with PDO to access the database.
Put our database connection information in Array, which is more convenient to add, and then use PDO to access it. Of course, this requires you to add the PDO module in php. The following is for reference:
public $db_info = array(
'username' => 'root',
' ' )
' 'other' =>array(
) {
new PDO("mysql:host=".$this->db_info[$db]['host'].';dbname='.$this->db_info[$db]['dbname'],$this ->db_info[$db]['username'],$this->db_info[$db] No introduction, it will be introduced in detail next time. Let me share some experiences below.
In the current process of using the MVC framework, I have found that unless there are complex requirements on SQL in backend development, the interactions at the Model and Controller layers are becoming more and more simple and repetitive, while the frontend is more and more responsible. The more tasks there are, the more diverse the data interaction between the
View and the Controller layer will be, and the front desk needs to do more data processing than before.
So it’s not that the backend is superior to frontend development, we will become more and more aware of the importance and complexity of frontend development.
Of course I still know too little and don’t have enough experience. I hope I can discuss and share with you all.
The above has introduced and shared a small summary of the use of the PHP CI framework, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.