Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of MVC of CI Framework

Shulou Source: shulou.com Published: 2022-06-03 19:50:09 11月12日 Update

This article will explain in detail the example analysis of the MVC of the CI framework. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

The details are as follows:

The simplest CI model:

Note: the model requires a database

Configuration file in appcation/config.php

We need to use the database here, so we need to fill in the relevant parameters in databases.php. I won't repeat them any more.

Go straight to the topic:

MVC:

1. First talk about the "M" model.

The models in CI are stored in the application/models folder

The naming rule is: class name _ model.php

There is only one class in the file:

Such as:

Class Nb_model extends CI_Model {public function _ construct () {/ / Connect database $this- > load- > database ();} public function get () {/ / query database $query=$this- > db- > get ('users'); / / return query results return $query- > result_array () as an array;}}

2. Secondly, talk about "C"

With the database model and its method, it is time for us to extract the data

The controller in CI is stored in the application/controllers folder

Naming convention: class name .php

Such as:

/ / prevent illegal 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 () {/ / get data according to the data model $data [' nb'] = $this- > nb_model- > get () / / load the view file $this- > load- > view ('nb',$data);}} / / comments at the end of the file / * End of file nb.php * / / * Location:. / application/controllers/nb.php * /

3. Finally, talk about "V"

With the database model and its method, it is time for us to extract the data

The controller in CI is stored in the application/controllers folder

Naming rules: class name .php (of course, it may not be a class name, as long as it matches the name of the parameter passed by view in the controller)

Such as:

This is the end of CI heiilo world's article on "sample Analysis of MVC in CI Framework". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Data model file database class name controller folder article rule control framework example analysis method more query good practical consistent topic Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Microsoft Shulou Technology Shulou Information Shulou Tech Info