


CI framework study notes (1) - environment installation, basic terminology and framework process, ci study notes_PHP tutorial
CI框架学习笔记(一) - 环境安装、基本术语和框架流程,ci学习笔记
最开始使用CI框架的时候,就打算写一个CI源码阅读的笔记系列,可惜虎头蛇尾,一直没有行动。最近项目少,总算是有了一些时间去写一些东西。于是准备将之前的一些笔记和经验记录下来,一方面权作备忘,另一方面时刻提醒自己:借鉴和学习才有出路,忘记过去意味着背叛!基本术语说明
在本文开始之前,有必要对文中反复出现的术语做一个简单的说明,如果你对这一部分已经熟谙,完全可以略过。本文中反复出现和提及的术语包括:
前端控制器(Front Controller):
用于集中控制用户的所有请求的组件,将用户的请求发送到具体的应用程序控制器。在CI框架中,指的就是框架的入口文件Index.php.前端控制器本身是一种设计模式,详细可参考《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文件, 配置如下:
$route['default_controller'] = "index"; $route['404_override'] = '';
2. 配置数据库database.php
如果你的应用程序需要提供动态内容,那么数据库几乎是必不可少的配置。打开application/config/database.php文件,该文件内容如下:
CI框架是支持多数据流连接的,default是当前默认的连接,active_record用于指定是否启用ARM(Active Record Model)。每个配置项非常简明,这里不再做过多介绍。
3. 去掉index.php
现在访问你的应用程序,url应该类似于这样:
test.xq.com/index.php/index test.xq.com/index.php/welcome
注意每个请求都会带有index.php段。去掉index.php会让URI更加美观。
打开刚刚添加的test.xq.com.conf文件,在server中添加如下配置:
if ($request_filename !~* /(favicon.ico|static|uploads|js|javascript|css|images|robots\.txt|index\.php|index\.html)) { rewrite ^/(.*)$ /index.php?$1 last; }
重启服务器后,现在,URL的访问方式变成了:
test.xq.com/index test.xq.com/welcome
是不是简洁多了 :D
4. 添加.html访问后缀
可能还有人喜欢url中添加特定的后缀,例如.html后缀使你的应用程序更类似于一系列静态文件。配置方法是,在application/config/config.php中,更改如下配置为:
$config['url_suffix'] = '.html';
CI框架的更多配置可以参考:
让Nginx支持.htaccess(本文没有提及使用.htaccess重写的内容,可以参考之)http://www.php100.com/html/program/nginx/2013/0905/5537.htmlCI框架集成Smarty,习惯用smarty模板引擎的童鞋可以看看 http://www.kankanews.com/ICkengine/archives/70302.shtml 配置Vhost
为了方便访问(相比ip地址访问的方式,域名访问有更好的可记忆性),我们可以配置vhost,配置方式为:进入nginx的vhost目录,新建配置文件(本文中为test.xq.com.conf,一般情况下,我们的每个vhost都会以域名命名)。在配置文件中输入如下内容:
server { listen 80; server_name test.xq.com; root /usr/nginx/html/CI/; access_log logs/xq_access_log main; error_log logs/testsq.log error; charset GBK; index index.php; location ~ .*\.(php|php5)?$ { include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass 127.0.0.1:9000; } }
Server中暂时没有其他rewrite配置,稍后在配置CI框架的时候,我们可以添加更多的配置类支持CI的友好URL.
打开本地的host文件,在host中添加条目:
10.130.130.130 test.xq.com
其中10.130.130.130应该是你的服务器的IP地址。
现在,在浏览器中可以通过域名访问CI框架了。
框架流程
在结束本文之前,我们再看看CI框架的基本流程,这个流程将贯穿源码阅读的始终,所以,很有必要认真研读一下。引用CI框架用户手册的上的流程图:
基本的执行流程如下:
Index.php是前端控制器,初始化框架所需的所有资源,加载应用程序基本配置,接收所有用户的请求,并通过Route路由用户请求若缓存文件存在,它将绕过通常的执行顺序,直接发送到客户端。Security数据过滤。这位于应用程序控制器装载之前。应用程序控制器加载数据库驱动、类库、业务逻辑类和可能的其他资源,处理用户的请求视图发送到客户端。如果开启缓存,则视图会被缓存,用于之后的请求。
常规的方法是ul嵌套,即主菜单ul-li里嵌套子菜单ul,要用到两级循环
首先循环主菜单,要有固定的条件来判断出主菜单,比如主菜单的uid==0或者其它。。。
- 栏目名称
if($news_item['uid'] == 0){ //判断并得到主菜单
echo " - ".$news_item['title'] . '
- ';
- "."ss".$child_item['title']."";
}
endforeach;
echo "";
}
endforeach; ?>
当然这仅限于两级菜单,多级或无限极,可以使用函数递归
function menu($uid=0){ //设置缺省从主菜单开始
global $news;
foreach ($news as $news_item):
if($news_item['uid'] == $uid){
echo " - ".$news_item['title'] . '
- ';
menu($news_item['id']); //递归调用
echo "";
}
endforeach;
}
------ 调用方法 ------------------------------- >
Zend Framework takes a lot of time and is not suitable for quick learning.
There are many framework programs at home and abroad now, such as speedphp, qeephp, cakephp, TP, etc.
According to the poster’s request, then there is only CI. Personally I think Pretty good,
About CodeIgniter
CodeIgniter is an application development framework and toolkit for PHP website developers. It provides a rich set of standard libraries and simple interfaces and logical structures, aiming to enable developers to develop projects more quickly. Use CodeIgniter to reduce the amount of code you write and focus your energy on the creative development of your projects.
CodeIgniter was developed by Rick Ellis, CEO of Ellislab. Its core framework is specially written for this program, while many other class libraries, auxiliary functions and subsystems come from the content management system ExpressionEngine written by Rick Ellis and Paul Burdick. Inspiration from Ruby on Rails inspired us to create a PHP framework and bring the concept of frameworks into the general consciousness of the web community.
She is a small but powerful PHP framework. As a simple and "elegant" toolkit, she can build fully functional web applications for PHP programmers. If you are a developer who shares your hosting and is worried about client deadlines, if you are tired of those stupid and clunky frameworks, then CodeIgniter is what you need if...
* You Want a compact frame.
* You need great performance.
* You need broad compatibility with various PHP versions and configurations on standard hosts (e.g. PHP4).
* You want a framework that requires almost 0 configuration.
* You want a framework that doesn't require the command line.
* You want a framework that doesn't have to adhere to restrictive coding rules.
* You are not interested in large-scale integration libraries like PEAR.
* You don't want to be forced to learn a template language (although you can choose the template parser you ask for).
* You don’t like complexity and love simplicity.
* You need clear, complete documentation.
The most important thing is that CI’s documentation is simple, rich and easy to understand, haha
If you want to learn, you can go to CI China and have a look, you don’t need me to post the address for you
foreach ($news as $child_item): //循环二次
if($news_item['id'] == $child_item['uid']){ //判断并得到对应子菜单
echo " - "."ss".$child_item['title']."";

To protect the application from session-related XSS attacks, the following measures are required: 1. Set the HttpOnly and Secure flags to protect the session cookies. 2. Export codes for all user inputs. 3. Implement content security policy (CSP) to limit script sources. Through these policies, session-related XSS attacks can be effectively protected and user data can be ensured.

Methods to optimize PHP session performance include: 1. Delay session start, 2. Use database to store sessions, 3. Compress session data, 4. Manage session life cycle, and 5. Implement session sharing. These strategies can significantly improve the efficiency of applications in high concurrency environments.

Thesession.gc_maxlifetimesettinginPHPdeterminesthelifespanofsessiondata,setinseconds.1)It'sconfiguredinphp.iniorviaini_set().2)Abalanceisneededtoavoidperformanceissuesandunexpectedlogouts.3)PHP'sgarbagecollectionisprobabilistic,influencedbygc_probabi

In PHP, you can use the session_name() function to configure the session name. The specific steps are as follows: 1. Use the session_name() function to set the session name, such as session_name("my_session"). 2. After setting the session name, call session_start() to start the session. Configuring session names can avoid session data conflicts between multiple applications and enhance security, but pay attention to the uniqueness, security, length and setting timing of session names.

The session ID should be regenerated regularly at login, before sensitive operations, and every 30 minutes. 1. Regenerate the session ID when logging in to prevent session fixed attacks. 2. Regenerate before sensitive operations to improve safety. 3. Regular regeneration reduces long-term utilization risks, but the user experience needs to be weighed.

Setting session cookie parameters in PHP can be achieved through the session_set_cookie_params() function. 1) Use this function to set parameters, such as expiration time, path, domain name, security flag, etc.; 2) Call session_start() to make the parameters take effect; 3) Dynamically adjust parameters according to needs, such as user login status; 4) Pay attention to setting secure and httponly flags to improve security.

The main purpose of using sessions in PHP is to maintain the status of the user between different pages. 1) The session is started through the session_start() function, creating a unique session ID and storing it in the user cookie. 2) Session data is saved on the server, allowing data to be passed between different requests, such as login status and shopping cart content.

How to share a session between subdomains? Implemented by setting session cookies for common domain names. 1. Set the domain of the session cookie to .example.com on the server side. 2. Choose the appropriate session storage method, such as memory, database or distributed cache. 3. Pass the session ID through cookies, and the server retrieves and updates the session data based on the ID.


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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.