背景:Cakephp開發環境版本是2.3.8,服務器的cakephp版本是2.3.5,測試好的代碼上傳到服務器上后發現總爆如下的警告:
Warning (2): strtolower() expects parameter 1 to be string, array given [CORE/Cake/Network/CakeRequest.php, line 478]
經過比對2.3.8和2.3.5發現,2.3.8比2.3.5的function is多了如下的代碼:
if (is_array($type)) { $result = array_map(array($this, 'is'), $type); return count(array_filter($result)) > 0; }雖然通過直接修改lib里的文件就能解決問題,但考慮到以后升級等問題,于是決定自定義這個CakeRequest,重寫is函數。過程如下:
在app/Config/bootstrap.php中加入如下代碼:
require APP . 'Lib' . DS . 'Network' . DS . 'CakeRequest.php';//筆者的環境下不把這個也引進來的話,會導致Error: Class 'CakeRequest' not found require APP . 'Lib' . DS . 'Network' . DS . 'AppCakeRequest.php';
在app/Lib/目錄下新建Network目錄, 將庫里(lib/Cake/Network/)的CakeRequest.php拷貝至這個目錄中,
然后在這個目錄里添加AppCakeRequest.php:
<?php/** * A class that helps wrap Request information and particulars about a single request. * PRovides methods commonly used to introspect on the request headers and request body. * * Has both an Array and Object interface. You can access framework parameters using indexes: * * `$request['controller']` or `$request->controller`. * * @package Cake.Network */class AppCakeRequest extends CakeRequest {/** * Check whether or not a Request is a certain type. Uses the built in detection rules * as well as additional rules defined with CakeRequest::addDetector(). Any detector can be called * as `is($type)` or `is$Type()`. * * @param string $type The type of request you want to check. * @return boolean Whether or not the request is the type you are checking. */ public function is($type) { //add str if (is_array($type)) { $result = array_map(array($this, 'is'), $type); return count(array_filter($result)) > 0; } //add end $type = strtolower($type); if (!isset($this->_detectors[$type])) { return false; } $detect = $this->_detectors[$type]; if (isset($detect['env'])) { if (isset($detect['value'])) { return env($detect['env']) == $detect['value']; } if (isset($detect['pattern'])) { return (bool)preg_match($detect['pattern'], env($detect['env'])); } if (isset($detect['options'])) { $pattern = '/' . implode('|', $detect['options']) . '/i'; return (bool)preg_match($pattern, env($detect['env'])); } } if (isset($detect['param'])) { $key = $detect['param']; $value = $detect['value']; return isset($this->params[$key]) ? $this->params[$key] == $value : false; } if (isset($detect['callback']) && is_callable($detect['callback'])) { return call_user_func($detect['callback'], $this); } return false; } }
編輯app/webroot/index.php:
/*$Dispatcher->dispatch( new CakeRequest(), new CakeResponse());*/$Dispatcher->dispatch( new AppCakeRequest(), new CakeResponse(array('charset' => Configure::read('App.encoding'))));大功告成,錯誤不再爆了,偶是cakephp新手,歡迎拍磚。
參考:http://stackoverflow.com/questions/8554536/extending-the-cakerequest-object
最后吐槽一下,今天終于算是把這個項目做完了,半年前應朋友的邀,接下這個Cakephp的網站項目,
雖然工期很輕松,足足給了我大半年的時間,而且朋友的需求也是斷斷續續給我的。
以前從來沒接觸過php,更別說cakephp,而且國內cakephp的資料比較少,很多問題都得去英文或者日文雅虎上找答案。
有時候真的想shi的心都有啊。
這實在是太浪費自己的碎片時間了,以后再也不會接這種私活了。
有空余時間不如學習下自己感興趣的知識不是?
新聞熱點
疑難解答