Asf_Http_Request

Class synopsis

<?php
final class Asf_Http_Request implements Asf_Http_RequestInterface
{
    public Asf_Http_Request function __construct(string $base_uri = '', string $request_uri = '')

    /* The "is*" start methods, Checks whether the HTTP methods is Get/Post/Put/Delete/Patch/... */
    /* Using the PHP Superglobal $_SERVER ["REQUEST_METHOD"], The value is case-insensitive */

    public boolean function isGet(void)
    public boolean function isPost(void)
    public boolean function isPut(void)
    public boolean function isDelete(void)
    public boolean function isPatch(void)
    public boolean function isHead(void)
    public boolean function isOptions(void)
    public boolean function isCli(void)
    public boolean function isXmlHttpRequest(void)
    public boolean function isTrace(void)
    public boolean function isConnect(void)
    public boolean function isFlashRequest(void)
    public boolean function isPropFind(void)

    /* Get specific index values for URL parameters */
    public mixed function getParam(string $name, mixed $default)

    /* Get all URL parameters */
    public array function getParams(void)

    /* Get the current module name */
    public string function getModuleName(void)

    /* Get the current service name */
    public string function getServiceName(void)

    /* Get the current action name */
    public string function getActionName(void)

    /* Get the current method name */
    public string function getMethod(void)

    /* Get the current base URI (asf.base_uri) */
    public string function getBaseUri(void)

    /* Get the current request URI */
    public string function getRequestUri(void)

    /* Get variable from superglobal $_GET */
    public array function getQuery(void)

    /* Get variable from superglobal $_POST */
    public array function getPost(void)

    /* Get variable from superglobal $_REQUEST */
    public array function getRequest(void)

    /* Get variable from superglobal $_FILES */
    public array function getFiles(void)

    /* Get variable from superglobal $_COOKIE */
    public array function getCookie(void)

    /* Get variable from superglobal $_SERVER */
    public array function getServer(void)

    /* Get ENV name for asf.env in php.ini */
    public string function getEnv(void)

    /* Check whether $_POST superglobal has certain index */
    public boolean function hasPost(string $name)

    /* Check whether $_GET superglobal has certain index */
    public boolean function hasQuery(string $name)

    /* Check whether $_SERVER superglobal has certain index */
    public boolean function hasServer(string $name)

    /* Check whether $_FILES superglobal has certain index */
    public boolean function hasFiles(string $name)
}

Examples

Example #1

<?php
try {
    $request = new Asf_Http_Request('requestUri', 'baseUri', false);
    print_r($request);
} catch (TypeError $e) {
    var_dump($e->getMessage());
}

$request = new Asf_Http_Request('xxxxxxxxxxxxxxxx');
print_r($request);

echo '------is value-------', PHP_EOL;
var_dump($request->isGet());
var_dump($request->isPost());
var_dump($request->isPut());
var_dump($request->isDelete());
var_dump($request->isPatch());
var_dump($request->isHead());
var_dump($request->isOptions());
var_dump($request->isCli());
var_dump($request->isXmlHttpRequest());
var_dump($request->isTrace());
var_dump($request->isConnect());
var_dump($request->isFlashRequest());
var_dump($request->isPropFind());

echo '------get value-------', PHP_EOL;
var_dump($request->getServer("xxx"));
var_dump($request->getEnv("xxx"));
var_dump($request->getModuleName());
var_dump($request->getServiceName());
var_dump($request->getActionName());
var_dump($request->getMethod());
var_dump($request->getBaseUri());
var_dump($request->getRequestUri());

echo '------default value-------', PHP_EOL;
var_dump($request->getQuery("xxx", 'defaultQuery'));
var_dump($request->getServer("xxx", 'defaultServer'));
var_dump($request->getPost("xxx", 'defaultPost'));
var_dump($request->getCookie("xxx", 'defaultCookie'));
var_dump($request->getEnv("xxx", 'defaultEnv'));

echo '------params-------', PHP_EOL;
var_dump($request->getParam('xxxx'));
var_dump($request->getParams());

echo '------has-------', PHP_EOL;
var_dump($request->hasPost('xxx'));
var_dump($request->hasQuery('xxx'));
var_dump($request->hasServer('xxx'));
var_dump($request->hasFiles('xxx'));

Example #2

<?php
$request = new Asf_Http_Request('xxxxxxxxxxxxxxxx');

if ($request->isPost()) {
    echo 'isPost', PHP_EOL;
}

/* GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS, CLI, TRACE, CONNECT ... */
$request->getMethod();

Example #3

<?php
class Bootstrap
{
    public function _initHow($dispatcher)
    {
        $request = $dispatcher->getRequest();
        if ($request->isXmlHttpRequest()) {
            // ... ...
        }
    }
}

$handle = new Asf_Application($configs);
$handle->bootstrap();

Example #4

<?php
$configs = array(
    'asf' => array(
        'root_path' => realpath(dirname(__FILE__)),
    )
);

class IndexService
{
    public function indexAction()
    {
        $request = Asf_Dispatcher::getInstance()->getRequest();
        var_dump($request);
    }
}

$handle = new Asf_Application($configs);
$handle->run();