如何看待PHP Strict Standards错误
前天无意修改了php.ini关于错误输出的设置,今天测试一个CakePHP开发的项目时竟然发现多了几条错误提示,虽然不是致命的但也不能忽视。
- Strict Standards: Redefining already defined constructor for class Object in D:\www\hosts\cake\ucake-libs\cake\libs\object.php on line 69
- Strict Standards: Assigning the return value of new by reference is deprecated in D:\www\hosts\cake\ucake-libs\cake\libs\object.php on line 94
- Strict Standards: Assigning the return value of new by reference is deprecated in D:\www\hosts\cake\ucake-libs\cake\libs\security.php on line 48
- Strict Standards: Assigning the return value of new by reference is deprecated in D:\www\hosts\cake\ucake-libs\cake\libs\inflector.php on line 65
- Strict Standards: Assigning the return value of new by reference is deprecated in D:\www\hosts\cake\ucake-libs\cake\libs\configure.php on line 89
- Strict Standards: Non-static method Configure::getInstance() should not be called statically in D:\www\hosts\cake\ucake-libs\cake\bootstrap.php on line 43
- Strict Standards: Non-static method Configure::write() should not be called statically in D:\www\hosts\cake\ucake-libs\cake\bootstrap.php on line 82
- Strict Standards: Non-static method Configure::getInstance() should not be called statically in D:\www\hosts\cake\ucake-libs\cake\libs\configure.php on line 108
打造CakePHP Ajax请求Action
在CakePHP的开发模式下,也就是Debug为2的时候,CakePHP会自动显示Sql查询语句及程序执行时间。当要用到Ajax去请求Action的时候难免会被这些语句干扰,使Javascript难解析出Ajax请求后的数据。为此可以这样来定义Ajax请求的action。
- function ajaxaciton() {
- $this->layout = '';
- // 禁止自动Render,免去为此Action去建View的烦扰
- $this->autoRender = false;
- // 手动定义为运营模式,去除debug信息
- Configure::write("debug", 0);
- //以utf-8的文本模式输出
- header("Content-type: text/plain; charset=utf-8");
- /*您的程序*/
- // 输出整理后的数据
- echo $message;
- }
解决CakePHP OthAuth组件更新用户登录时间(last_visit)的错误
- Query: INSERT INTO `users` (`last_visit`,`created`,`modified`) VALUES ("2008-10-27 07:50:17","2008-10-27 07:50:17","2008-10-27 07:50:17")
- Warning: SQL Error: 1062: Duplicate entry '' for key 2 in D:\www\hosts\cake\ucake-libs\cake\libs\model\datasources\dbo_source.php on line 439
此错误出现的原因是:用户登录成功后更新最后登录时间时($this->controller->{$this->user_model}->save($row,true,array($this->user_table_last_visit));)丢失其要修改记录的ID造成。
解决办法:在此行(大概在134行)前面添加下面代码
- $this->controller->{$this->user_model}->id = $this->user("id");
学会使用CakePHP的cache函数
cache($path, $data = null, $expires = "+1 day', $target = "cache')(此函数位于CakePHP核心目录basic.php文件中)
函数功能大概的解释为:缓存数据与取出缓存的数据。
cache函数允许我们传递四个参数:
1,$path: 缓存文件的路径与名称;
2,$data: 要缓存的数据(当为null时为取出缓存数据);
3,$expires: 数据缓存时间(格式:GNU Date Input Formats 语法);
4,缓存的类型(默认为CACHE,一般保留默认即可)。 查看详细内容…
CakePHP使用redirect后会停止执行后面的代码吗
CakePHP中$this->Model->redirect(url)后会执行其后面的代码吗?为此我进行了如下测试:
- function index() {
- $this->redirect("/news/view/");
- $this->redirect("/news/edit/");
- }
执行上面代码,没有按我的预计想法执行(跳转到view里),而是跳转到了edit里。由此可见$this->Model->redirect(url);后CakePHP并没有为我们加入exit();方法,它会继续执行其后的代码。
所以建议使用中,如果$this->Model->redirect(url);后我们不想让其后面的代码执行的话,应该手动为其加上exit();方法,即:$this->Model->redirect(url);exit(); ($this->render(view)同)。
CakePHP Session->setFlash使用方法
- $this->Session->setFlash("Session数据");
- if($session->check("Message.flash")) $session->flash();
CakePHP如何手动验证表单数据
View中使用$html->input('Content/title');来生成的表单可以用<?php echo $html->tagErrorMsg('Content/title', "请填写标题');?>来显示出错信息
要使这种验证生效可以这样:Model中加$validate = array('title" => VALID_NOT_EMPTY);/*VALID_NOT_EMPTY也可以换为正则表达式*/
Controller相对应的action中应该这样if(!$this->Content->validates($this->data)) {$this->render();}
