Introduction to the CI framework: analysis of the CI structure and how CI works

Source: Internet
Author: User
Hello everyone! In the previous section, we deployed a CI website, which is very simple. However, you need to know that this is just the beginning ~~~ Hello everyone! In the previous section, we deployed a CI website, which is very simple. However, you need to know that this is just the beginning ~~~


In this section, we mainly want to understand the file structure of CI and how it works. This is especially important!


[Content of this lecture]

I,CI File structure;
Understanding the CI file structure can help us quickly understand the CI framework as if we were in a strange city, everything around you is unfamiliar and unknown. to quickly learn about the city, you can buy a map of the city, overall understanding of the city's location, structure and landscape, and so on.


II,How CI works.
We should not only have a general understanding of the CI framework, but also understand how CI works so that we can quickly master and use CI. let's talk about it in a strange city just now, if you want to quickly adapt to and integrate into the city, do we need to know the customs and customs of the city. I think it is quite necessary. after all, we have to follow the customs.
Of course, June is just a brief introduction here to give everyone a general idea.

[Details]
1. CI file structure.
Do you still remember the CI directory structure in the first lecture? at that time, I didn't explain it in detail. let's take a look.





As we can see, the main component of CI is, Application), System (system folder)And Index. php entry file.


The application folder mainly stores controllers, models, and views. The system folder mainly stores the core files that constitute the CI, index. A php Portal file is a single portal file. a single file refers to a file in a website (application) that all requests point, it is responsible for receiving and processing controllers and methods in URLs. In other words, it calls a 'controller' and returns a 'view '.


The specific introduction to a single portal file I cited an explanation of Gao Luofeng in BroPHP. As follows:

Single Portal file:


When using PHP procedural programming, each PHP file can be accessed and run independently. just as a stadium has multiple entrances, you need to check tickets and perform security checks at each entrance. The single portal mode is used for project deployment and access. no matter what functions are completed, a project has only one unified (but not necessarily the only) portal, just like a stadium, if it can only be controlled from one entrance (the program is abstract, one entrance and multiple entrances are equally efficient), it is more flexible and has almost no disadvantages. The main portal file has the following advantages:


1. convenient file loading
When writing and reading procedural program code, files are often included among them, including the use of include in PHP, including libraries and public resource files, and the use of include in HTML. And script to load CSS and javaScript files. The larger the project, the more files, the more headaches you may feel. just like a large network, the files are intertwined, making it hard to find a clue. The use of a single portal solves this problem. any file used in the project can be searched relative to the location of a single portal file.


2. easy permission verification
If each php file can be accessed independently, you need to judge each file during user permission verification. To use a single portal, you only need to judge at one position.


3. simple URL rewriting
If each php file and the PHP file in different directories can be accessed independently, many rules are required for URL rewriting on the Web server. Using a single entry requires only a few simple rules during URL rewriting.


Next, let's take a look at the files in the application (application folder) and system (system folder) and what their functions are.


Application:
The cache is empty when it is installed for the first time. if you enable the cache settings, this directory stores the cache data.

Config stores the configuration file, including the basic configuration information of the website.
Controllers stores the controller directory of your project
Core this directory can expand the system's core files
Errors contains the error information page. you do not need to modify this directory.
Hooks is empty for the first installation and is used to store the hooks you have created. Hooks are used to load other files.
Helpers helper functions. you can extend the helper functions of the system.
Language: The file directory that stores your own language

Libraries class library, you can create your own class library
Logs if you enable the system error log, the log file is saved in this directory by default.
Models stores the model directory of your project
View Template directory for storing views


System:
Core stores system core files

Database CI framework database class library file
Fonts does not introduce the font used to store watermark images in the user manual.
Helpers helper functions. you can extend the helper functions of the system.
The English language directory.

Libraries stores directories of some class libraries, such as SESSION, paging, and image.



In the application folder, the most important folder is config, which contains two files to be concerned: config. php and database. php.
The following are the controllers, models, and views folders, which respectively store controllers, models, and views on your website.


You may wonder why CI needs to set the file structure in this way. actually, there is no reason to put the code in this directory instead of that directory, this is a convention in CI.


In addition, do you find that some folders of application and system are the same, such as core, helpers, and libraries. In fact, this is also agreed by the CI structure. when you load an auxiliary function helper, or the library of the class library file, CI will search in the above two directories, for example, if you want to load a class called unlimited classification class Category, CI will first find the application/libraries Directory. If this directory does not exist, CI will look for the system/libraries Directory. This means that the libraies and helpers of the CI core library can be replaced by placing files of the same name in the application directory. But do not try this easily, because this high flexibility requires you to have enough CI experience.



2,How does CI work?


In the previous section, we quickly set up a CI website, and the browser successfully displays a welcome page. We can't help wondering how it is displayed? In fact, according to our previous introduction to CI and structure analysis, we can not find that this is related to CI using M-V-C mode and a single entry file.


Let's take a simple analysis:


For example, when we access http: // localhost/ci_demo/index. in php, the program will rely on index. php performs a lot of initialization work, calls a lot of base class libraries, and according to the index. php parameters to load controllers and methods, then call the model, load View and other content information. Of course, in this example, index. php does not see any parameters, but this does not mean that no parameters exist, because CI has already set parameters for controllers and methods by default, and this default parameter can be specified by itself, the configuration file is stored in application/config/routes. the configuration file contains the following settings:

$ Route ['default _ controller'] = "welcome"; $ route ['0000_override'] = ''; in the preceding config file, the default controller is welcome, if no method is specified, the index method is specified by default. If the requested controller or method does not exist, the program will go to the "404" page. Result:





In addition, the method (also called a function) in the above URL must exist in the previous controller. Or you cannot call methods in other controllers in one controller.


Let's summarize the details of how CI processes URLs. (Part from the CI China Forum ):

Assume that the URL is: Http: // yoursite/index. php/control/func/param1/param2 /...



URL snippet Purpose
Http://www.yoursite.com
Locate the basic URL of your website
/Index. php
Locate the CI router and read other parts of the URL. after analysis, you will be directed to the relevant webpage.
/Control
The name of the controller that CI will call (if no controller name is set, CI will call the default controller you set in the config file)
/Func
The name of the function that CI will call is located in the called controller. (If this function does not exist, the index function is called by default, unless you use _ remap)
/Param1
CI uses this as the variable passed to the function
If/param2 /...
CI transmits more parameters as variables to the function

Therefore, the above URL can be understood: H Ttp: // localhost/index. php/controller name/method parameter 1/method parameter 2 /...


Summary:The explanation of how the specific CI works is simple and general. The main purpose is to quickly bring you a preliminary understanding of CI, more will be further explored later.


Well, let's talk about it. Next, let's learn how to write a controller, method, and view, then let's write an example that everyone knows -- "Hello World! ",

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.