Google News
logo
Phalcon - Interview Questions
What is Request Component in Phalcon?
Phalcon\Http\Request is a component that encapsulates the actual HTTP request (usually originated by a browser) and sent to our application. The Phalcon\Http\Request object is a simple value object that is passed between the dispatcher and controller classes, wrapping the HTTP request environment. It also offers easy access to information such as header data, files, method, variables etc.
<?php

use Phalcon\Http\Request;

$request = new Request();

// POST
if (true === $request->isPost()) {
    // AJAX
    if (true === $request->isAjax()) {
        // ....
    }
}
Getting Values : PHP automatically fills the superglobal arrays $_GET, $_POST and $_REQUEST depending on the type of the request. These arrays contain the values present in forms submitted or the parameters sent via the URL. The variables in the arrays are never sanitized and can contain illegal characters or even malicious code, which can lead to SQL injection or Cross Site Scripting (XSS) attacks.
 
Phalcon\Http\Request allows you to access the values stored in the $_GET, $_POST and $_REQUEST arrays and sanitize or filter them with the filter service.
 
There are 5 methods that allow you to retrieve submitted data from a request :
 
* get()
* getQuery()
* getPost()
* getPut()
* getServer()

All (except getServer()) accept the following parameters :
 
* name the name of the value to get
* filters (array/string) the sanitizers to apply to the value
* defaultValue returned if the element is not defined (null)
* notAllowEmpty if set (default) and the value is empty, the defaultValue will be returned; otherwise null
* noRecursive applies the sanitizers recursively in the value (if value is an array)
<?php

use Phalcon\Http\Request;

$request = new Request();

$request->get(
    $name = null,            // string
    $filters = null,         // mixed
    $defaultValue = null,    // mixed
    $notAllowEmpty = false,  // bool
    $noRecursive = false     // bool
): mixed​
Advertisement