国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

extends CakeRequest,重寫is方法

2019-11-15 01:32:09
字體:
來源:轉載
供稿:網友
extends CakeRequest,重寫is方法

背景: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的心都有啊。

這實在是太浪費自己的碎片時間了,以后再也不會接這種私活了。

有空余時間不如學習下自己感興趣的知識不是?


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东兰县| 丹巴县| 积石山| 商洛市| 龙南县| 宜黄县| 汝阳县| 宕昌县| 韩城市| 平山县| 阜阳市| 甘洛县| 丰镇市| 朝阳市| 浮山县| 兴国县| 大安市| 广宗县| 无棣县| 盐城市| 漯河市| 芦溪县| 新疆| 白沙| 阳江市| 循化| 修武县| 德庆县| 都兰县| 孝义市| 牙克石市| 墨脱县| 永平县| 镇巴县| 堆龙德庆县| 辽源市| 壤塘县| 宜兰县| 聂拉木县| 永年县| 乌苏市|