CodeIgniter getting started Article 1: Information Publishing, ci
I. MVC
CodeIgniter adopts the MVC Architecture: control layer, model layer, and view layer.
Corresponding to the folder under the Application (Figure 1 ):
All new files end with. php
View layer view folder in HTML Template
Model Layer model stores database operation code
The control layer controllers stores the code for logical judgment, obtains data from the model layer, inputs the data to the view layer, and sends it to the user.
Figure 1
Function:
1. Add an input form to the template.
2. The controller adds code for receiving form data and performs simple verification on user input.
3. output the title, body, and release time at the top of the form.
Knowledge points used: CI helper class (url) and input class (input ),
And CI ActiveRecord and pass the value to the template.
Ii. Initial Configuration
1. Link to the database
Modify database Configuration:/application/config/database. php
'hostname' => 'localhost', 'username' => 'root', 'password' => '', 'database' => 'test', 'dbdriver' => 'mysqli', 'dbprefix' => 'ts_',
2. Modify the default route
The CI framework uses a single file portal. By default, the access control layer must be accessed through index. php. For example, the controllers folder contains a class named test, and test has a function called home,
The access URL is: http://www.example.com/index.php/test/home
3. Output page
1. Directly output the HTML Template
Create two files in the controllers folder and the views folder respectively.
Test. php
<? Phpdefined ('basepath') OR exit ('no direct script access allowed'); class Test extends CI_Controller {public function home () {$ this-> load-> view ('home') ;}} home. php <? Php defined ('basepath') OR exit ('no direct script access allowed');?> <! DOCTYPE html>
Open a URL similar to the following in a browser: http://test.com/index.php/test/home
2. Insert database entries
Create Database Table ts_news
Test. php
<? Phpdefined ('basepath') OR exit ('no direct script access allowed'); class Test extends CI_Controller {public function _ construct () {parent :__ construct (); $ this-> load-> helper ('url'); $ this-> load-> model ('news _ model');} public function home () {$ this-> load-> view ('home');} public function add_news () {$ title = $ this-> input-> get ('title ', TRUE); $ content = $ this-> input-> get ('content'); if (strlen ($ title) <20) or (Strlen ($ content) <20) {echo 'title or body content too short '; return false;} $ arr = array ('id' => '', 'title' => $ title, 'content' => $ content, 'Update _ time' => time (), 'create _ time' => time ()); $ check = $ this-> news_model-> insert ($ arr, 'News'); if ($ check) {redirect ('test/home ');} else {echo 'submission failed' ;}} home. php <? Php defined ('basepath') OR exit ('no direct script access allowed');?> <! DOCTYPE html>
Modify the body of the Home template:
<Body>
Refresh and view results: