


CI framework source code reading notes 1, ci framework source code notes_PHP tutorial
CI framework source code reading notes 1, ci framework source code notes
When I first started using the CI framework, I planned to write a series of notes on CI source code reading, but unfortunately it ended in an anticlimactic manner. , there has been no action. There have been few projects recently, so I finally have some time to write something. So I prepared to record some of my previous notes and experiences, on the one hand as a memo, and on the other hand, I always reminded myself: the only way out is to learn from others, and forgetting the past means betrayal!
Basic terminology explanation
Before starting this article, it is necessary to make a brief explanation of the terms that appear repeatedly in the article. If you are already familiar with this part, you can completely skip it. Terms that appear and are mentioned repeatedly in this article include:
Front Controller:
A component used to centrally control all user requests and send user requests to specific application controllers. In the CI framework, this refers to the CodeIgniter class. The front-end controller itself is a design pattern. For details, please refer to "J2EE Design Patterns".
Application Controller
The application controller is a specific controller that processes user request URLs. A group of related processes or requests are usually placed in an application controller. For example: UserController may contain user registration, verification, personal information, Personal page and other related operations.
MVC
A common term is a code layering and organization model. Divide the code into M (Model, business logic), V (view, view), C (Controller, controller) and other levels to facilitate the separation of the business logic part and the view rendering part and reduce the coupling of the code. Currently, many frameworks in PHP are based on the MVC pattern, such as ZF, YII, CI, etc.
RouteRoute
Although it is called Route, it is not a router, but refers to the process of intercepting the user's request and forwarding the request to a specific Controller for processing. Different frameworks have different routing, but the basic principles are the same.
HookHook
The original Hook refers to "a link in message delivery, used to monitor the delivery of messages and add specific processing before message processing." Hook here refers to adding or changing the core functions of the system without changing the core source code of the framework. The most typical situations include: running a specific script before the controller is loaded or after the loading is completed.
CI framework configuration
The basic environment of this article: Linux x86_64 GNU/Linux. PHP(CGI)+Nginx+Mysql+redis is installed (so many server-related configurations in this article are based on Nginx, and the Apache server is temporarily ignored. ).
First download the source code of the CI framework. The download address is: http://codeigniter.org.cn/downloads. The current stable version is 2.2.0. Unzip the source code to a folder (assumed to be the /usr/nginx/html/CI directory).
Before configuring the CI framework, first browse the directory structure of the framework:
Among them:
Application : The directory of the application. All your application code should be located in this directory
index.php : The entry file of the framework
static : The directory we created ourselves to place some static files such as CSS, image and js (this can be placed in the application directory, depending on personal preference)
system : The system file of the CI framework is also the main part of source code reading
user_guide : User guide, similar to an offline user manual.
The CI framework requires less configuration:
1. Configure routes
The default application controller and 404 page are configured in Routes.php. Open the application/config/routes.php file and configure it as follows:
<span>$route</span>['default_controller'] = "index"<span>; </span><span>$route</span>['404_override'] = '';
2. Configuration database database.php
If your application needs to provide dynamic content, then a database is almost an essential configuration. Open the application/config/database.php file. The content of the file is as follows:
The CI framework supports multi-data stream connections, default is the current default connection, and active_record is used to specify whether to enable ARM (Active Record Model). Each configuration item is very concise and will not be introduced in detail here.
3. Remove index.php
Now access your application, the url should look like this:
<span>test.xq.com/index.php/index test.xq.com/index.php/welcome</span>
注意每个请求都会带有index.php段。去掉index.php会让URI更加美观。
打开刚刚添加的test.xq.com.conf文件,在server中添加如下配置:
<span>if ($request_filename !~* /(favicon.ico|static|uploads|js|javascript|css|images|robots\.txt|index\.php|index\.html)) { rewrite ^/(.*)$ /index.php?$</span>1 last<span>; </span>}
重启服务器后,现在,URL的访问方式变成了:
<span>test.xq.com/index test.xq.com/welcome</span>
是不是简洁多了 :D
4. 添加.html访问后缀
可能还有人喜欢url中添加特定的后缀,例如.html后缀使你的应用程序更类似于一系列静态文件。配置方法是,在application/config/config.php中,更改如下配置为:
<span>$config</span>['url_suffix'] = '.html';
CI框架的更多配置可以参考:
配置Vhost
为了方便访问(相比ip地址访问的方式,域名访问有更好的可记忆性),我们可以配置vhost,配置方式为:进入nginx的vhost目录,新建配置文件(本文中为test.xq.com.conf,一般情况下,我们的每个vhost都会以域名命名)。在配置文件中输入如下内容:
<span>server { listen </span>80<span>; </span> server_name test.xq.com<span>; </span> root /usr/nginx/html/CI/<span>; </span><span> access_log logs/xq_access_log main</span><span>; </span> error_log logs/testsq.log error<span>; </span> charset GBK<span>; </span> index index.php<span>; </span><span> location ~ .*\.(php|php5)?$ { include fastcgi_params</span><span>; </span> fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name<span>; </span> fastcgi_pass 127.0.0.1:9000<span>; </span><span> } }</span>
Server中暂时没有其他rewrite配置,稍后在配置CI框架的时候,我们可以添加更多的配置类支持CI的友好URL.
打开本地的host文件,在host中添加条目:
10.130.130.130 test.xq.com
其中10.130.130.130应该是你的服务器的IP地址。
现在,在浏览器中可以通过域名访问CI框架了。
框架流程
在结束本文之前,我们再看看CI框架的基本流程,这个流程将贯穿源码阅读的始终,所以,很有必要认真研读一下。引用CI框架用户手册的上的流程图:
基本的执行流程如下:
下一步开始,将开始CI的源码阅读之旅。
这是类里面的一个方法
功能是:浏览器使用post请求发送 shop_id到服务器,这个 shop_id 就是数据库中的manufacture_id,根据这个shop_id查询数据库,把查到的记录以json格式字符串的形式输出到浏览器
提示Fatal error: Class 'Test_model' not found in D:\wamp\www\CodeIgniter_2.1.2\system\core\Loader.php on line 303
意思提示这个Test_model类找不到
你的类名写错了,当然找不到了
以下为model代码,文件名为test_model.php: (类名要与文件名保持一致才行)
class Test_m extends CI_Model{ // 最好要大写都大写改成test_model
以下为contraller代码,文件名为user.php
$this->load->model('test_model'); 它加载的时候找不到class test_model
这样就应该能成功

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
