CodeIgniter记录错误日志的方法全面总结_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
<?php echo Test::$test2; //直接使用类中的值 ?>
这种直接使用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程序设计有所帮助。

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

AI Hentai Generator
Generate AI Hentai for free.

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



Pinduoduo software provides a lot of good products, you can buy them anytime and anywhere, and the quality of each product is strictly controlled, every product is genuine, and there are many preferential shopping discounts, allowing everyone to shop online Simply can not stop. Enter your mobile phone number to log in online, add multiple delivery addresses and contact information online, and check the latest logistics trends at any time. Product sections of different categories are open, search and swipe up and down to purchase and place orders, and experience convenience without leaving home. With the online shopping service, you can also view all purchase records, including the goods you have purchased, and receive dozens of shopping red envelopes and coupons for free. Now the editor has provided Pinduoduo users with a detailed online way to view purchased product records. method. 1. Open your phone and click on the Pinduoduo icon.

How to open the error log in PHP: 1. Open the php.ini configuration file, find the "log_errors" option, and change the value of this option from "Off" to "On". 2. Use the ini_set() function to modify the PHP file, with the syntax "ini_set("log_errors", "On");".

How to View Command History in Linux In Linux, we use the history command to view the list of all previously executed commands. It has a very simple syntax: history Some options for pairing with the history command include: Option description -c clears the command history for the current session -w writes the command history to a file -r reloads the command history from the history file -n Limit the number of output of recent commands Simply run the history command to see a list of all previously executed commands in a Linux terminal: In addition to viewing command history, you can also manage command history and perform modifications to previously executed commands , reverse search command history or even delete history completely

How to implement custom middleware in CodeIgniter Introduction: In modern web development, middleware plays a vital role in applications. They can be used to perform some shared processing logic before or after the request reaches the controller. CodeIgniter, as a popular PHP framework, also supports the use of middleware. This article will introduce how to implement custom middleware in CodeIgniter and provide a simple code example. Middleware overview: Middleware is a kind of request

Summary of frequently asked questions about importing Excel data into Mysql: How to deal with error log problems encountered when importing data? Importing Excel data into a MySQL database is a common task. However, during this process, we often encounter various errors and problems. One of them is the error log issue. When we try to import data, the system may generate an error log listing the specific information about the error that occurred. So, how should we deal with the error log when we encounter this situation? First, we need to know how

Call recording in iPhone is often underestimated and is one of the most critical features of iPhone. With its simplicity, this feature is of vital importance and can provide important insights about the calls made or received on the device. Whether for work purposes or legal proceedings, the ability to access call records can prove invaluable. In simple terms, call history refers to the entries created on your iPhone whenever you make or receive a call. These logs contain key information, including the contact's name (or number if not saved as a contact), timestamp, duration, and call status (dialed, missed, or not answered). They are a concise record of your communication history. Call history includes call history strips stored on your iPhone

CodeIgniter Middleware: Accelerating Application Responsiveness and Page Rendering Overview: As web applications continue to grow in complexity and interactivity, developers need to use more efficient and scalable solutions to improve application performance and responsiveness. . CodeIgniter (CI) is a lightweight PHP-based framework that provides many useful features, one of which is middleware. Middleware is a series of tasks that are performed before or after the request reaches the controller. This article will introduce how to use

iPhone lets you add medications to the Health app to track and manage the medications, vitamins and supplements you take every day. You can then log medications you've taken or skipped when you receive a notification on your device. After you log your medications, you can see how often you took or skipped them to help you track your health. In this post, we will guide you to view the log history of selected medications in the Health app on iPhone. A short guide on how to view your medication log history in the Health App: Go to the Health App>Browse>Medications>Medications>Select a Medication>Options&a
