search
HomeWeb Front-endJS TutorialCodeIgniter记录错误日志的方法全面总结_php实例

本文实例讲述了CodeIgniter记录错误日志的方法。分享给大家供大家参考,具体如下:

CI工作流程:

所有的入口都从根目录下的index.php进入,确定应用所在目录后,加载 codeigniter/CodeIgniter.php 文件,该文件会顺序加载以下文件执行整个流程。

index.php:检测文件路径,加载codeigniter.php文件

codeigniter.php: 加载 Common/constants....文件。获取文件模式、设置计时器、实例化类(错误类、扩展类、钩子类、系统扩展、配置类、编码类、路由类、过程类、输出类、安全类、语言类、控制器)、加载请求方法、渲染输出view。

CodeIgniter的一个类会保存为一个php文件,类名与文件名同名,它的核心应用类会在类名前加"CI_"。

system/core/common.php:包含检测php版本、文件权限、加载核心类、获取配置参数、加载异常/错误类、获取http请求状态等公共函数

application/config/constants.php:设置文件权限常量、应用程序宏定义文件

system/core/Benchmark.php:用来记录执行时间

system/core/Hooks.php:检测是否有钩子对象调用

system/core/Config.php:为管理配置文件提供方法,检测application/config/config.php参数

application/config/config.php:配置全局参数

system/core/URI.php:解析url参数

system/core/Router.php:检测路由配置,解析 HTTP 请求,以确定谁来处理

system/core/Output.php:检查是否有缓存文件,如果存在则直接输出内容。

system/core/Input.php:过滤 HTTP 请求和任何用户提交的数据

system/core/Long.php:初始化提示语言变量

system/core/conctroller.php:控制输出类

记录错误日志:

默认程序不记录错误日志,如果有需要的话可以设置:

1、在application/config/config.php中设置:

$config['log_threshold'] = 1//(可设置:1/2/3/4)

如果为0表示不输出错误日志,具体可查看里面的介绍;

2、在需要写入错误的页面调用全局函数log_message('级别','消息'),级别有三个,一是error,即php运行错误,二是debug,系统调试,CI本身在很多页面也加了自己的系统debug,三是info,介绍运行中的一些消息,消息内容自己写;

3、默认情况下错误日志存放在application/logs/log-[time].php中,它按日期存放文件,比如:log-2011-6-26表示存入今天的日志内容,一般情况下为了隐藏日志内容须将这个地址挪位,可以在$config['log_path']中设置路径,按要求最好是完整路径信息。

设置自己的全局变量/配置:

有时需要定义自己的全过程变量以供在其它地方使用,如自定义的session等,在CI中这项工作也很轻松。

1、在application/config/中创建自己的config文件,注意存放文件位置。比如建立一个自己的配置文件mysetting.php,内容,

$config['try'] = 'this is my trying';

2、在需要调用自定义全局变量的地方使用$this->config->load('settingfile')函数,比如:

$this->config->load('mysetting');

如果有需要也可以通过application/config/autoload.php设置为自动加载。

3、接下来在同一页面中使用

$this->config->item('varname')

函数,比如:$this->config->item('try');会输出:this is my trying;
上面可以看出,CI中函数调用为:$this->filename的形式,也可以看出CI把整个系统看成一个大的类,然后通过加载、继承等方式获取相应方法。
更多自定义变量参考:http://codeigniter.org.cn/user_guide/libraries/config.html

隐藏index.php与加载外部文件:

其实不管是在用CI还是ZF都有同样一个问题,就是路径的问题。前期,我在用ZF做CMS时,我在.htaccess文件中设置了如遇到js,css,img等资源文件都不重定向。但今天在用CI时,却忘记了,弄了半天都没搞好,登陆CI的中国官方网,终于在论坛高手的帮助下把问题觖决了,在这里把它贴出来,供大家分享。

首先,隐藏url中的index.php文件,这样访问其它目录的时候就不会有http://www.xxx.com/index.php/xxx的样式出现,面是直接http://www.xxx.com/xxx形式,在根目录.htaccess文件里设置(作用是隐藏index.php,有时index.php可能不在根目录,则htaccess须移到index.php所在目录),如下:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|js|css|robots\.txt)
#这里排除了images、js、css目录及index.php、robots.txt文件
RewriteRule ^(.*)$ index.php/$1 [L]

这里JS,CSS,IMG等资源文件夹与SYSTEM文件夹放在同一级下,独立放置的好处是不用受htaccess的限制,因为htaccess文件写明Deny from all,即拒绝访问。打开application/config/config.php改写配置:

$config['base_url'] = "http://127.0.0.1/";
$config['index_page'] = "index.php";

如果

$config['base_url'] = http://127.0.0.1;

后面没加'/',则在model_rewrite最后一行应写RewriteRule ^(.*)$ /index.php/$1 [L],在index.php前加一个'/'。然后在JS文件夹中建立ajax.js文件,我在VIEW层中的文件为index.html。这样我要引入JS时,可以用CI自带的BASE_URL来设置,如下:

在controllers里相关控制网页里添加(在其它load之前):

$this->load->helper('url');

在views表现的index.html里:

复制代码 代码如下:

注:这里url是网站相对URL(好处是可以更改根目录后相对地址不用改变)

这里js文件夹没有重定向,所以可以正常访问,而如果是受限制的页面则比较麻烦了。

好了,CI中引入外部的JS与CSS就这么简单。

注别的说明:“ RewriteCond $1 !^(index\.php|images|js|css|robots\.txt) ”这里代码的意思是:任意你想访问的资源都不被重定向时,都可写在这里。有时,网站没有加载CSS,JS(它的路径都是正确的)时,都是被重定向了,这要注意。

具体可查看CI的中国官论坛 http://codeigniter.org.cn/user_guide/helpers/url_helper.html,URL辅助函数一节,
http://codeigniter.org.cn/user_guide/general/urls.html,url设置,
http://codeigniter.org.cn/forums/thread-4-1-2.html,Hex关于隐藏index.php的说明,但他在model_rewrite用了index\\.php,我觉得用双反斜杠有误。

(另外:特别谢谢CI中国官论坛上的Hex 与visvoy )

数据间的传输:

1、将数据从控制器传入视图

由于控制器controllers在ci中扮演交通警察的角色,其是一个大类,而视图view作为controller类中的一个函数中的函数,所以view可以使用controller中的属性。所以可以这样写:

Controller类Test

class Test extends CI_Controller {
 public static $test2=''; //定义一个属性
 public function __construct(){
 parent::__construct();
 self::$test2 = $this->load->view('new','',true); //给$test2这个属性赋值
 }
 public function index() {
 $this->load->helper('url');
 $this->load->view('anchor');
 }
}

View.php

<&#63;php
echo Test::$test2; //直接使用类中的值
&#63;>

这种直接使用controllers类中的值的方法虽然可行,却不是ci所提倡的。一般来说在controller中使用$this->load->view()的时候可以通过参数传值给view视图:

function index()
{
 $data['css'] = $this->css;
 $data['base'] = $this->base;
 $data['mytitle'] = 'Welcome to this site';
 $data['mytext'] = "Hello, $name, now we're getting dynamic!";
 $this->load->view('testview', $data); //$data通过参数传递到view
}

这里,把需要传递的数值加入至$data数组,ci在核心类中给自动使用extract()函数把数组“解压”出来,成为一个个变量。所以在view中可以直接这样使用变量:

echo $css;

2、模型与视图的交互

在ci中模型总是用以处理数据,模型中数据处理也是通过controller中转到view,所以最好不要试图模型直接与视图联系。手册中有这样一个例子:

class Blog_controller extends CI_Controller {
 function blog() {
 $this->load->model('Blog'); //载入模型
 $data['query'] = $this->Blog->get_last_ten_entries(); //使用模型中的方法,将返回值存入$data数组
 $this->load->view('blog', $data); //像上例一样,通过参数传给视图view
 }
}

更多关于CodeIgniter相关内容感兴趣的读者可查看本站专题:《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《php优秀开发框架总结》、《ThinkPHP入门教程》、《ThinkPHP常用方法总结》、《Zend FrameWork框架入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家基于CodeIgniter框架的PHP程序设计有所帮助。

Statement
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
Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

See all articles

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 Article

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment