Home > Backend Development > PHP Tutorial > A comprehensive summary of the method of recording error logs in CodeIgniter, codeigniter recording log_PHP tutorial

A comprehensive summary of the method of recording error logs in CodeIgniter, codeigniter recording log_PHP tutorial

WBOY
Release: 2016-07-12 08:52:12
Original
1000 people have browsed it

Comprehensive summary of CodeIgniter's method of recording error logs, codeigniter's log recording

The example in this article describes how CodeIgniter records error logs. Share it with everyone for your reference, the details are as follows:

CI workflow:

All entrances are entered from index.php in the root directory. After determining the directory where the application is located, load the codeigniter/CodeIgniter.php file, which will load the following files in sequence to execute the entire process.

index.php: Detect the file path and load the codeigniter.php file

codeigniter.php: Loads the Common/constants.... file. Get file mode, set timer, instantiate class (error class, extension class, hook class, system extension, configuration class, encoding class, routing class, process class, output class, security class, language class, controller), load Request method, render output view.

A class of CodeIgniter will be saved as a php file. The class name has the same name as the file name. Its core application class will have "CI_" in front of the class name.

system/core/common.php: Contains public functions such as detecting PHP version, file permissions, loading core classes, obtaining configuration parameters, loading exception/error classes, and obtaining http request status

application/config/constants.php: Set file permission constants, application macro definition files

system/core/Benchmark.php: used to record execution time

system/core/Hooks.php: Detect whether there is a hook object call

system/core/Config.php: Provides methods for managing configuration files and detects application/config/config.php parameters

application/config/config.php:Configure global parameters

system/core/URI.php: Parsing url parameters

system/core/Router.php: Detect routing configuration and parse HTTP requests to determine who should handle them

system/core/Output.php: Check whether there is a cache file, and if it exists, output the content directly.

system/core/Input.php: Filters HTTP requests and any user-submitted data

system/core/Long.php: Initialize prompt language variable

system/core/controller.php:Control output class

Record error log:

The default program does not record error logs, you can set it if necessary: ​​

1. Set in application/config/config.php:

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

If it is 0, it means no error log will be output. Please refer to the introduction inside for details;

2. Call the global function log_message('level','message') on the page where errors need to be written. There are three levels. One is error, which is PHP running error. The second is debug, system debugging. CI itself has many functions. The page also adds its own system debug. The third is info, which introduces some messages during operation. The message content is written by yourself;

3. By default, the error log is stored in application/logs/log-[time].php. It stores files by date. For example: log-2011-6-26 means that today’s log content is stored. In general In order to hide the log content, this address must be moved. You can set the path in $config['log_path']. It is best to use the complete path information as required.

Set your own global variables/configuration:

Sometimes you need to define your own process variables for use in other places, such as customized sessions, etc. This work is also very easy in CI.

1. Create your own config file in application/config/, and pay attention to the location of the file. For example, create your own configuration file mysetting.php, content,

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

2. Use the $this->config->load('settingfile') function where you need to call custom global variables, such as:

$this->config->load('mysetting');
Copy after login

If necessary, you can also set it to automatic loading through application/config/autoload.php.

3. Next use

on the same page
$this->config->item('varname')
Copy after login

Function, for example: $this->config->item('try'); will output: this is my trying;
As can be seen from the above, the function call in CI is in the form of: $this->filename. It can also be seen that CI regards the entire system as a large class, and then obtains the corresponding information through loading, inheritance, etc. method.
For more custom variable reference: http://codeigniter.org.cn/user_guide/libraries/config.html

Hide index.php and load external files:

In fact, whether you are using CI or ZF, you have the same problem, which is the path problem. In the early days, when I was using ZF as a CMS, I set in the .htaccess file that resource files such as js, css, img, etc. would not be redirected. But when I was using CI today, I forgot about it and couldn't get it right after a long time. I logged into CI's official Chinese website and finally solved the problem with the help of forum experts. I posted it here for everyone to share.

首先,隐藏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]

Copy after login

这里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";

Copy after login

如果

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

后面没加'/',则在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');

Copy after login

在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');
 }
}

Copy after login

View.php

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

Copy after login

这种直接使用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
}

Copy after login

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

echo $css;

Copy after login

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
 }
}

Copy after login

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

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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1127869.htmlTechArticleCodeIgniter记录错误日志的方法全面总结,codeigniter记录日志 本文实例讲述了CodeIgniter记录错误日志的方法。分享给大家供大家参考,具体如下...
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template