extends CakeRequest, override is method_PHP tutorial

WBOY
Release: 2016-07-13 10:32:55
Original
1010 people have browsed it

Background: The Cakephp development environment version is 2.3.8, and the server’s cakephp version is 2.3.5. After the tested code was uploaded to the server, the following warning always appeared:

Warning (2): strtolower() expects parameter 1 to be string, array given [CORECakeNetworkCakeRequest.php, line 478]

After comparing 2.3.8 and 2.3.5, it was found that 2.3.8 has more functions than 2.3.5 with the following code:

        <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>
Copy after login

Although the problem can be solved by directly modifying the files in the lib, considering future upgrades and other issues, I decided to customize this CakeRequest and rewrite the is function.
The process is as follows:

Add the following code to 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';
Copy after login

Create a new Network directory under the app/Lib/ directory, and copy the CakeRequest.php in the library (lib/Cake/Network/) to this directory,

Then add AppCakeRequest.php:

in this directory
<?<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>
Copy after login

Edit 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>
Copy after login

It's done, the error no longer pops up, I am new to cakephp, welcome to try it.

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

Finally, I want to complain. I finally finished this project today. I took on this Cakephp website project at the invitation of a friend half a year ago,

Although the construction period was very easy, it gave me more than half a year, and my friends’ needs were also met intermittently.

I have never been exposed to PHP before, let alone cakephp, and there is relatively little information on cakephp in China. Many questions have to be found on English or Japanese Yahoo.

Sometimes I really want to do something.

This is such a waste of my time, and I will never take on such private work again.

If you have free time, why not study the knowledge that interests you?

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/753004.htmlTechArticleBackground: The Cakephp development environment version is 2.3.8, the server’s cakephp version is 2.3.5, the tested code After uploading to the server, I found the following warning: Warning (2): strtolower()...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template