A simple example of MVC in the CI framework
This article provides a simple example of MVC for getting started with the CI framework. We will share this with you for your reference. The details are as follows:
The simplest CI model:
Note: databases are required for models.
The configuration file is in appcation/config. php
Here we need to use the database. We need to fill in the relevant parameters in databases. php.
Enter the topic directly:
MVC:
1. First talk about the "M" Model
Models in CI are stored in the application/models folder.
The naming rule is: Class name_model. php
The file contains only one class:
For example:
Class Nb_model extends CI_Model {public function _ construct () {// connect to the database $ this-> load-> database ();} public function get () {// query the database $ query = $ this-> db-> get ('users '); // return the query result in an array: return $ query-> result_array ();}}
2. Talk about "C"
With the database model and its method, we should extract data.
The Controller in CI is stored in the application/controllers folder.
Naming rules: Class Name. php
For example:
// Prevent unauthorized access to if (! Defined ('basepath') exit ('no direct script access allowed'); class Nb extends CI_Controller {public function _ construct () {parent :__ construct (); // load the data model $ this-> load-> model ('nb _ model');} public function index () {// obtain data based on the data model $ data ['nb'] = $ this-> nb_model-> get (); // load the view file $ this-> load-> view ('nb', $ data) ;}// comment at the End of the file/* End of file nb. php * // * Location :. /application/controllers/nb. php */
3. Finally, let's talk about "V"
With the database model and its method, we should extract data.
The Controller in CI is stored in the application/controllers folder.
Naming rules: Class Name. php (of course, it can not be a class name, as long as it is the same as the name of the parameter passed by view in the Controller)
For example:
<Html>