A simple example of MVC in the CI framework

Source: Internet
Author: User
Tags php foreach

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> 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.