PHP Development Framework--codeigniter (CI) Usage Summary ____php

Source: Internet
Author: User
Tags codeigniter

Turn from: http://blog.sina.com.cn/s/blog_7b60d05f0101tl9h.html

In the development of the framework is very important, there are many frameworks, WPF, more well-known MVVM, etc., which are used to organize the development of documents, is to write an application separately. The following is a summary of the use of the CI framework.
The CI framework is based on MVC, respectively

Models: Model, used to display entity classes, tool classes, database access classes, etc., belong to the background of data support and type support.

Controls: Controller, as a traffic policeman's role, is to control the internal data and the external interface of the coordination, so this is in the middle of M and V to do the role of regulation, it is a regulatory role, then need to master some things: how and M communication, how and V exchange, and how users are communicating with themselves. These are the core, more important things, the following will be summed up.

Views: View, this is the interface, feedback to the user to operate the interface, mainly in HTML to write, this everyone understand.

Here are a few steps to introduce and summarize the use of CI:
1, preparation work.
2, File Introduction.
3, M, C, v communication between the way.

(a) Download, address http://codeigniter.org.cn/Download Complete is the framework of the application, and then placed under the server www file, look at the file PHP development framework, the following figure:

(b) Then there are two folders, one is application, this is the application file place, go inside to look at:

PHP Development Framework –codeigniter (CI) Use summary here you can see MVC, controllers,models,views separate files, and then have a config folder to install the configuration file.
Go back to the system folder there are some usage classes written by the team that has developed the framework, and we don't have to modify it to use it directly. Very simple, very clear.
(c) Here is the point, in learning to use a framework, the elements of the framework between the communication method must be clear, so that can be engaged. First of all, the idea of the CI framework, the user is accessed through a controller method to access a page, so this controller plays a very important role, said it is a traffic police, responsible for monitoring the internal data changes and feedback interface, Accept the interface feedback internal data and other functions.

Here are a few major communications:

1. The user communicates with controller, unexpectedly the user is accesses the page by the method in C, then the user communicates with the method through the parameter, the user can write the parameter directly in the URL, in order, for example http://localhost/CI/index.php/ Hello/sayhello/jarvin/afternoon
Here is a visit to the SayHello method in the controller of Hello, see method:

PHP Development Framework –codeigniter (CI) Usage Summary
I think it's clear that the first parameter Jarvin the second parameter afternoon.

2.controller communication with view, although said to be able to mix HTML to interface expression, but it is to use MVC to separate, into the view inside, in the Views folder to write good HTML files, these files can be full page HTML can also be part of the HTML, For example, the head, body, tail and so on. After writing, you can use Controller to refer to views in the view to show things. , it is important to be able to refer to the interface file in controller, and then how to call it:
$this->load->view ("text_view.php"); Add this sentence to the function to refer to a file in a views. If you want to call multiple, such as a head, body, tail call go in, then directly below add more than two of the same, change the name on OK, but also an important thing is controller how to communicate with the view? The answer is to take advantage of the array and then pass in the array after the first reference. See examples below:

function Show ()
    {
        $name = "Jarvin";
        $count =1;
        $data = Array (' Name ' => $name, ' num ' => $count);  
        $this->load->view ("text_view.php", $data);//Call a view
    }

So pass through, and then how to answer it, the following figure:

3.Controller communication with model.
The data used to submit to the view is often not written dead, then the dynamic data will be used in the database, so long to use the model. , the following summarizes the production of a model class for the database to be deleted.
In fact, controller calls the functions in the model class, and then passes in the arguments to communicate in the form of C and V communication.

The first is to create a new model class, and the model for creating a new model class is:

Class ClassName extends ci_model//note to inherit Ci_model class
{
    function __construct ()
    {
        parent::__construct () ;//constructor to invoke the constructor of the parent class
        $this->load->database ();//load the database, the database name is configured inside the config file. , and other information, such as password, and/or
write functions below.
}

The test model class is given below:

<?php
class Test_m extends Ci_model
{
    function __construct ()
    {
        parent::__construct ();
        $this->load->database ();
    }

    function User_insert ($arr)
    {
        $this->db->insert ("person", $arr);
    }

    function User_update ($name, $arr)
    {
        $this->db->where ("name", $name);
        $this->db->update ("person", $arr);
    }
    function User_delete ($name)
    {
        $this->db->where ("name", $name);
        $this->db->delete ("person");
    }
    function User_select ($name)
    {
        $this->db->where ("name", $name);
        $this->db->select ("*");
        $query =  $this->db->get ("person");
        return $query->result ();
    }
? >

Then create a new controller inside the controllers to use the functionality of this model class:

<?php if (! defined (' BasePath ')) exit (' No Direct script access allowed ');

Class User extends Ci_controller 
{//inheritance is required.
    function Insert ()
    {
        $this->load->model (' test_m ');
        $arr =array (' Name ' => ' fff ', ' age ' =>21);
        $this->test_m->user_insert ($arr);
    }
    function Update ()
    {
        $this->load->model (' test_m ');
        $arr =array (' Name ' => ' gg ', ' age ' =>12);
        $this->test_m->user_update (' J ', $arr);
    }
    function Delete ()
    {
        $this->load->model (' test_m ');
        $this->test_m->user_delete ("Ken");
    function  Select ()
    {
        $this->load->model (' test_m ');
        Var_dump ($this->test_m->user_select ("GGG"));
    }
? >

OK, just study it. Very simple

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.