Home > Article > Backend Development > How CodeIgniter records error logs
This article mainly introduces the method of CodeIgniter recording error logs, analyzes the file structure and corresponding functions of the CodeIgniter framework in detail, analyzes the implementation techniques of CodeIgniter framework recording error logs with examples, and analyzes hidden index files and data. The summary of transmission and other techniques is very comprehensive. Friends who need it can refer to
. The example in this article describes the method of CodeIgniter recording error logs. Share it with everyone for your reference, the details are as follows:
CI workflow:
All entries are entered from index.php in the root directory, determine the directory where the application is located Finally, load the codeigniter/CodeIgniter.php file, which will load the following files sequentially to execute the entire process.
index.php: Detect the file path and load the codeigniter.php file
codeigniter.php: Load 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 detecting php version, file permissions, loading core classes, obtaining configuration parameters, loading exception/error classes, obtaining http request status, etc. Public functions
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: Provide methods for managing configuration files and detecting application/config/config.php parameters
application/config/config.php: Configuring global parameters
system/core/URI.php: Parse url parameters
system/core/Router.php: Detect routing configuration and parse HTTP requests to determine who will handle it
system/core/Output.php: Check whether there is a cache file, and if it exists, output the content directly.
system/core/Input.php: Filter HTTP requests and any user-submitted data
system/core/Long.php: Initialization Prompt language variable
system/core/controller.php:Control output class
Record error log:
Default program Do not record error logs. If necessary, you can set it:
1. Set it in application/config/config.php:
$config['log_threshold'] = 1//(可设置:1/2/3/4)
If it is 0, it means no error log is output. For details, please see the introduction inside;
2. Call the global function log_message('level','message') on the page where errors need to be written. There are three levels, one The first one is error, that is, the PHP running error. The second one is debug, system debugging. CI itself has also added its own system debug on many pages. The third one 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. Generally, it is hidden. The log content must move this address. 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 full process variables for use in other places, such as customized sessions, etc., in CI This job is also very easy.
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';
2. Use $this->config where you need to call custom global variables. ->load('settingfile') function, for example:
$this->config->load('mysetting');
If necessary, it can also be set through application/config/autoload.php Load automatically.
3. Next, use the
$this->config->item('varname')
function on the same page, 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 looks at the entire system. into a large class, and then obtain the corresponding methods through loading, inheritance, etc.
More custom variable reference: http://codeigniter.org.cn/user_guide/libraries/config.html
Hide index.php and load external files:
其实不管是在用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里:
复制代码 代码如下:
0270b8e0a32ec3ebfa0e13d8dfa1eff2">2cacc6d41bbb37262a98f745aa00fbf0
注:这里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 } }
相关推荐:
CI框架(CodeIgniter)实现的数据库增删改查操作总结
The above is the detailed content of How CodeIgniter records error logs. For more information, please follow other related articles on the PHP Chinese website!