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

这实在是太浪费自己的碎片时间了,以后再也不会接这种私活了。

有空余时间不如学习下自己感兴趣的知识不是?

你可能感兴趣的:(request)