Core\Request::isHead
Core\Request::isHead(): boolChecks whether the request is a HEAD request. The result is true for HEAD and false for every other method.
A client sends HEAD when it wants the headers of a response but not its body. A link checker does this to see whether a page exists. Novis runs a HEAD request the same way as a GET request, and the server does not send the body. So Core\Request::method returns Get for both, and isHead is the only way to tell them apart.
You do not need to check for HEAD for your program to be correct. Check it when building the body is expensive and you want to skip that work, or when a HEAD request should not count as a visit.
A command-line program answers no request, so isHead throws a LogicError there.
Return value
Returns bool. true when the request method is HEAD, and false for every other method. The server sends no body in the response to a HEAD request, so a program can skip the work of building one.
Errors
LogicErrorThe program is not handling a request.
Examples
Telling head from get
<?nvs// Shows how to tell a `HEAD` request from a `GET` request.
// The request below uses the method `HEAD`.// `Core\Request::method` returns `Get` for it. This prints "method is Get".if (Core\Request::method() == Core\Http\Method::Get) { echo "method is Get\n";}
// `Core\Request::isHead` returns `true` only for a `HEAD` request. This prints "isHead is true".echo 'isHead is ', Core\Request::isHead() ? 'true' : 'false', "\n";method is GetisHead is trueBuilding the page only when it is read
<?nvs// Shows how a program builds a page only when the client will read it.
// The server sends no body in the response to a `HEAD` request.// So a program can skip the work of building the page.if (Core\Request::isHead()) { return;}
// The request below uses `GET`, so `isHead` returns `false` and the page is built.// This prints three lines, one for each product.array<string> $products = ['Tea', 'Coffee', 'Cocoa'];foreach ($products as string $product) { echo '<li>', $product, "</li>\n";}<li>Tea</li><li>Coffee</li><li>Cocoa</li>Not counting a link checker
<?nvs// Shows how a page counts its visitors, but not the tools that only check that a link works.
// A link checker often sends a `HEAD` request. It does not read the page.uint $visits = 41;if (!Core\Request::isHead()) { $visits = $visits + 1;}
// The request below uses `HEAD`, so the count stays the same. This prints "visits: 41".// A real server sends no body for `HEAD`. The example prints the count so you can see it.echo 'visits: ', $visits, "\n";visits: 41