Yaf_Request_Abstract::isXmlHttpRequest

(Yaf >=1.0.0)

Yaf_Request_Abstract::isXmlHttpRequestDetermine if the request is an Ajax request

Description

public function Yaf_Request_Abstract::isXmlHttpRequest(): bool

Checks whether the request is an Ajax request by inspecting the HTTP_X_REQUESTED_WITH request header.

Note:

Yaf_Request_Simple overrides this method and always returns false, since command-line requests are never Ajax requests.

Parameters

This function has no parameters.

Return Values

Returns true if the request is an Ajax request (the X-Requested-With header equals XMLHttpRequest), false otherwise.

Examples

Example #1 Yaf_Request_Abstract::isXmlHttpRequest() example

<?php
class ProductController extends Yaf_Controller_Abstract
{
    public function viewAction()
    {
        // Assuming the request carries "X-Requested-With: XMLHttpRequest"
        if ($this->getRequest()->isXmlHttpRequest()) {
            $this->getResponse()
                 ->setHeader("Content-Type", "application/json")
                 ->setBody(json_encode(["id" => 17, "name" => "Yaf in Action"]));
            return false;
        }
    }
}
?>

The above example will output something similar to:

{"id":17,"name":"Yaf in Action"}

See Also

add a note

User Contributed Notes 1 note

up
0
oreaking at gmail dot com
11 years ago
That means the request method is ajax or not .