Table of Contents
CI框架源码阅读笔记1,ci框架源码笔记
基本术语说明
CI框架配置
配置Vhost
框架流程
谁可以解释下这段php代码ci框架的
php CI框架里遇到的问题
Home php教程 php手册 CI框架源码阅读笔记1,ci框架源码笔记

CI框架源码阅读笔记1,ci框架源码笔记

Jun 13, 2016 am 09:22 AM
php

CI框架源码阅读笔记1,ci框架源码笔记

  最开始使用CI框架的时候,就打算写一个CI源码阅读的笔记系列,可惜虎头蛇尾,一直没有行动。最近项目少,总算是有了一些时间去写一些东西。于是准备将之前的一些笔记和经验记录下来,一方面权作备忘,另一方面时刻提醒自己:借鉴和学习才有出路,忘记过去意味着背叛!

基本术语说明

  在本文开始之前,有必要对文中反复出现的术语做一个简单的说明,如果你对这一部分已经熟谙,完全可以略过。本文中反复出现和提及的术语包括:

前端控制器(Front Controller):

  用于集中控制用户的所有请求的组件,将用户的请求发送到具体的应用程序控制器。在CI框架中,指的是CodeIgniter类 。前端控制器本身是一种设计模式,详细可参考《J2EE设计模式》。

应用程序控制器

  应用程序控制器是具体的处理用户请求URL的控制器,通常将一组相关的处理或者请求放置在一个应用程序控制器中,例如:UserController可能包含用户的注册、验证、个人信息、个人页面等相关操作。

MVC

  老生常谈的一个术语,是一种代码分层和组织模式。将代码分为M(Model,业务逻辑),V(view ,视图),C(Controller,控制器)等层次,便于将业务逻辑部分和视图渲染部分分离,减少代码的耦合。目前PHP中许多框架都基于MVC模式,如ZF,YII,CI等

Route路由

  虽然名为Route,但这里并不是路由器,而是指截取用户的请求并将请求转发到特定的Controller处理的过程。不同的框架的路由不同,但基本原理相同。

Hook钩子

  最初的Hook是指“消息传递中一个环节,用于监控消息的传递,并在消息处理之前,添加特定的处理”。这里的Hook是指,在不改变框架核心源码的基础上增加或更改系统的核心功能,最典型的情况包括:在控制器加载之前或加载完成之后运行特定的脚本。

CI框架配置

  本文的基本环境:Linux  x86_64  GNU/Linux .安装了PHP(CGI)+Nginx+Mysql+redis(所以本文的许多服务器相关的配置都是以Nginx为主,而暂时忽略Apache服务器)。

  首先下载CI框架的源码,下载地址为:http://codeigniter.org.cn/downloads 目前稳定版本是2.2.0  。将源码解压到文件夹(假设为/usr/nginx/html/CI 目录)。

  配置CI框架之前,先浏览一下框架的目录结构:

其中:

Application :     应用程序的目录,你的所有的应用代码都应该位于这个目录

index.php  :     框架的入口文件

static         :     我们自己建立的目录,放置一些CSS,image和js等静态文件(这完全可以放到application目录下,看个人喜好)

system       :    CI框架的系统文件,也是源码阅读的主要部分

user_guide :        用户指导,类似于离线的用户手册。

CI框架需要配置的地方比较少:

1.  配置routes

  Routes.php中配置的是默认的应用程序控制器和404页面. 打开application/config/routes.php文件, 配置如下:

<span>$route</span>['default_controller'] = "index"<span>;
</span><span>$route</span>['404_override'] = '';
Copy after login

2.  配置数据库database.php

  如果你的应用程序需要提供动态内容,那么数据库几乎是必不可少的配置。打开application/config/database.php文件,该文件内容如下:

  CI框架是支持多数据流连接的,default是当前默认的连接,active_record用于指定是否启用ARM(Active Record Model)。每个配置项非常简明,这里不再做过多介绍。

3.  去掉index.php

  现在访问你的应用程序,url应该类似于这样:

<span>test.xq.com/index.php/index
test.xq.com/index.php/welcome</span>
Copy after login

注意每个请求都会带有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>}
Copy after login

重启服务器后,现在,URL的访问方式变成了:

<span>test.xq.com/index
test.xq.com/welcome</span>
Copy after login

是不是简洁多了 :D

4.  添加.html访问后缀

  可能还有人喜欢url中添加特定的后缀,例如.html后缀使你的应用程序更类似于一系列静态文件。配置方法是,在application/config/config.php中,更改如下配置为:

<span>$config</span>['url_suffix'] = '.html';
Copy after login

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>
Copy after login

Server中暂时没有其他rewrite配置,稍后在配置CI框架的时候,我们可以添加更多的配置类支持CI的友好URL.

打开本地的host文件,在host中添加条目:

10.130.130.130  test.xq.com   
Copy after login

其中10.130.130.130应该是你的服务器的IP地址。

现在,在浏览器中可以通过域名访问CI框架了。

框架流程

         在结束本文之前,我们再看看CI框架的基本流程,这个流程将贯穿源码阅读的始终,所以,很有必要认真研读一下。引用CI框架用户手册的上的流程图:

 

基本的执行流程如下:

下一步开始,将开始CI的源码阅读之旅。

 

谁可以解释下这段php代码ci框架的

这是类里面的一个方法
功能是:浏览器使用post请求发送 shop_id到服务器,这个 shop_id 就是数据库中的manufacture_id,根据这个shop_id查询数据库,把查到的记录以json格式字符串的形式输出到浏览器
 

php CI框架里遇到的问题

提示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

这样就应该能成功
 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles