[CodeIgniter II], an analysis of CI structure and how CI works

Source: Internet
Author: User
Tags extend watermark images codeigniter
I. The document structure of CI

Do you remember the CI directory structure in the first lecture, which was not detailed at the time, and we'll look at it again.

As we can see from the above figure, the main components of CI are application (Application folders), System (Systems folders), and index.php entry files.


The main application folder is to store the controller, model and view, and so on, the system folder is mainly to store the core files composing CI, the index.php entry file is a single entry file, the so-called single file refers to a Web site (application), all the requests are pointing to such a file, It is responsible for receiving and processing the controllers and methods in the URL. In other words, it calls a ' controller ' and returns a ' view '.

Introduction to a single entry file I quoted a Gaulo teacher's explanation of it in brophp. As follows:

Single entry file:
When using PHP procedural programming, each PHP file can be accessed and run independently, like a stadium with multiple entrances, requiring check-in and security checks at every entrance. With a single entry model for project deployment and access, no matter what function is accomplished, a project has only one uniform (but not necessarily only) entry, just like a stadium. If you can only enter from one entrance (the program is abstract, one entry and multiple entry efficiency is the same) control is more flexible, almost no shortcomings. The advantages of using the main portal file to deploy the project are as follows:

1. Easy to load file
When you write and read procedural code, you often encounter files that are included with each other, including PHP using include function libraries and public resource files, and also include using <link> and <script> in HTML Load CSS and JavaScript files. The larger the project, the more files the more people feel headache, like a large network of documents interwoven together, it is not easy to find a clue. Using a single entry solves this dilemma, and any file that is used in the project application, as long as it is located relative to the location of a single entry file.


2. Easy to verify authority
If each PHP file can be accessed independently, you need to judge each file when you do user rights validation. Using a single entry only needs to be judged in one place.

3, url rewrite simple
If each php file and php file in a different directory can be accessed independently, many rules are required to rewrite the URL in the Web server. With a single entry, the URL rewrite requires only a few simple rules.



OK, then let's look at what files are in the application (Application folder), System (Systems folder), and what their role is.

Application:
Cache is empty for the first time, if you turn on caching settings, this directory holds cached data
Config file containing the basic configuration information of the Web site
Controllers store your project's controller directory
Core This directory can extend the kernel files of the system
Errors contains error information page, you do not need to modify this directory
Hooks is empty when first installed, and is used to store the hooks you create. Hooks are the control method used to load other files.
Helpers auxiliary function, you can extend the auxiliary function of the system
Language file directory for your native language
Libraries class Library, you can create your own class library
Logs if you set the error log on the system open, the log file is saved by default in this directory
Models store your project's model directory
The template directory for the Views store view


System:
Core Storage System Kernel file
class library files for database classes in the DB CI framework
Fonts not described in the user manual, the font used to store watermark images
Helpers auxiliary function, you can extend the auxiliary function of the system
Language file directory for storing English
Libraries store some class library directory, such as session class, pagination class, image class, etc.


The most important folder in the

Application folder (application) is config, which has two files to focus on: config.php and database.php.
, followed by the controllers, models, and Views folders, which store the controllers, models, and view of your site, respectively.

Love asking questions you may wonder why CI has to set the file structure this way, in fact, why put the code in this directory rather than that directory is no good reason to say, this is a contract in CI.

Other careful you have found that application and system some folders are the same, such as core, helpers, libraries and so on. In fact, this is also by the CI structure, when you load an auxiliary function helper, or class library file library, CI will be found in the above two directories, such as you want to load a class called infinite classification of Class category, CI will first find application/ Libraries directory. If this directory does not exist, CI will look for the system/libraries directory. This means that the libraies, helpers, of the CI core library can be replaced by placing the file with the same name in the application directory. But don't try so hard, because this high degree of flexibility requires you to have enough experience with CI.

second, how CI is the work of
In the last section we quickly built a CI website, the browser successfully displayed a welcome interface. We can not help but ask how it has been shown. In fact, based on our previous introduction and structural analysis of CI, it is not difficult to find that this is related to CI using m-v-c mode and a single entry file.

Let's make a simple analysis:

For example, when we visit http://localhost/ci_demo/ index.php, the program will rely on index.php to do a lot of initialization work, call a large number of base class library, and according to index.php parameters to load controllers and methods, and then call the model, loading views and other content information. Of course in this example index.php you don't see any parameters, but this does not mean that there is no parameter exists, because CI has already specified the controller and method parameters by default, this default parameter can be specified, the configuration file is stored in the application/config/ In routes, the configuration file contains the following settings:

$route [' default_controller '] = "welcome"; $route [' 404_override '] = '; The config file above, the default controller is welcome, and 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. The result is as shown in the figure:


In addition, it is necessary to further explain that the Chinese method (also called a function) of the above URL must be present in the previous controller. Or, you cannot invoke a method within a controller on another controller.

Let's summarize the specific details of CI processing URLs (part of the CI China Forum):

If URL is: http://yoursite/index.php/control/func/param1/param2/...


URL Fragment Use
Http://www.yoursite.com
Locate the basic URL for your site
/index.php
Locate the CI router and read the rest of the URL, and after analysis, go to the relevant page
/control
The name of the controller that CI will call (if the controller name is not set, CI will invoke the default controller you set in the config file)
/func
The name of the function that CI will call, located within the controller being invoked. (If this function is not present, the index function is invoked by default unless you use _remap)
/param1
CI takes this as a variable passed to the function
If there are/param2/...
CI passes more arguments to the function as a variable

So the above URL can be understood as: H ttp://localhost:port/index.php/Controller name/Method name/Method parameter 1/method parameter 2/...

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.