>php教程 >php手册 >extends CakeRequest,重写is方法

extends CakeRequest,重写is方法

WBOY
WBOY원래의
2016-06-13 09:38:021559검색

背景: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多了如下的代码:

        <span if</span> (<span is_array</span>(<span $type</span><span )) {
            </span><span $result</span> = <span array_map</span>(<span array</span>(<span $this</span>, 'is'), <span $type</span><span );
            </span><span return</span> <span count</span>(<span array_filter</span>(<span $result</span>)) > 0<span ;
        }</span>

虽然通过直接修改lib里的文件就能解决问题,但考虑到以后升级等问题,于是决定自定义这个CakeRequest,重写is函数。
过程如下:

在app/Config/bootstrap.php中加入如下代码:

<span require</span> APP . 'Lib' . DS . 'Network' . DS . 'CakeRequest.php'<span ;//笔者的环境下不把这个也引进来的话,会导致</span><span <strong>Error: </strong> Class 'CakeRequest' not found <br />require</span> APP . 'Lib' . DS . 'Network' . DS . 'AppCakeRequest.php';

在app/Lib/目录下新建Network目录, 将库里(lib/Cake/Network/)的CakeRequest.php拷贝至这个目录中,

然后在这个目录里添加AppCakeRequest.php:

<?<span php
</span><span /*</span><span *
 * 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
 </span><span */</span>
<span class</span> AppCakeRequest <span extends</span><span  CakeRequest {

</span><span /*</span><span *
 * 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.
 </span><span */</span>
    <span public</span> <span function</span> is(<span $type</span><span ) {
        </span><span //</span><span add str</span>
        <span if</span> (<span is_array</span>(<span $type</span><span )) {
            </span><span $result</span> = <span array_map</span>(<span array</span>(<span $this</span>, 'is'), <span $type</span><span );
            </span><span return</span> <span count</span>(<span array_filter</span>(<span $result</span>)) > 0<span ;
        }
        </span><span //</span><span add end</span>
        <span $type</span> = <span strtolower</span>(<span $type</span><span );
        </span><span if</span> (!<span isset</span>(<span $this</span>->_detectors[<span $type</span><span ])) {
            </span><span return</span> <span false</span><span ;
        }
        </span><span $detect</span> = <span $this</span>->_detectors[<span $type</span><span ];
        </span><span if</span> (<span isset</span>(<span $detect</span>['env'<span ])) {
            </span><span if</span> (<span isset</span>(<span $detect</span>['value'<span ])) {
                </span><span return</span> env(<span $detect</span>['env']) == <span $detect</span>['value'<span ];
            }
            </span><span if</span> (<span isset</span>(<span $detect</span>['pattern'<span ])) {
                </span><span return</span> (bool)<span preg_match</span>(<span $detect</span>['pattern'], env(<span $detect</span>['env'<span ]));
            }
            </span><span if</span> (<span isset</span>(<span $detect</span>['options'<span ])) {
                </span><span $pattern</span> = '/' . <span implode</span>('|', <span $detect</span>['options']) . '/i'<span ;
                </span><span return</span> (bool)<span preg_match</span>(<span $pattern</span>, env(<span $detect</span>['env'<span ]));
            }
        }
        </span><span if</span> (<span isset</span>(<span $detect</span>['param'<span ])) {
            </span><span $key</span> = <span $detect</span>['param'<span ];
            </span><span $value</span> = <span $detect</span>['value'<span ];
            </span><span return</span> <span isset</span>(<span $this</span>->params[<span $key</span>]) ? <span $this</span>->params[<span $key</span>] == <span $value</span> : <span false</span><span ;
        }
        </span><span if</span> (<span isset</span>(<span $detect</span>['callback']) && <span is_callable</span>(<span $detect</span>['callback'<span ])) {
            </span><span return</span> <span call_user_func</span>(<span $detect</span>['callback'], <span $this</span><span );
        }
        </span><span return</span> <span false</span><span ;
    }
    
}</span>

编辑app/webroot/index.php:

<span /*</span><span 
$Dispatcher->dispatch(
    new CakeRequest(),
    new CakeResponse()
);
</span><span */</span>
<span $Dispatcher</span>-><span dispatch(
    </span><span new</span> AppCakeRequest(),
    <span new</span> CakeResponse(<span array</span>('charset' => Configure::read('App.encoding'<span )))
);</span>

大功告成,错误不再爆了,偶是cakephp新手,欢迎拍砖。

参考:http://stackoverflow.com/questions/8554536/extending-the-cakerequest-object

 

最后吐槽一下,今天终于算是把这个项目做完了,半年前应朋友的邀,接下这个Cakephp的网站项目,

虽然工期很轻松,足足给了我大半年的时间,而且朋友的需求也是断断续续给我的。

以前从来没接触过php,更别说cakephp,而且国内cakephp的资料比较少,很多问题都得去英文或者日文雅虎上找答案。

有时候真的想shi的心都有啊。

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

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

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.