2017 © Pedro Peláez
 

library request

Anax Request class.

image

anax/request

Anax Request class.

  • Monday, April 30, 2018
  • by mikael_roos
  • Repository
  • 1 Watchers
  • 2 Stars
  • 3,627 Installations
  • PHP
  • 25 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 10 Versions
  • 11 % Grown

The README.md

Anax Request

Latest Stable Version Join the chat at https://gitter.im/mosbth/anax, (*1)

Build Status CircleCI, (*2)

Build Status Scrutinizer Code Quality Code Coverage, (*3)

Maintainability Codacy Badge, (*4)

Anax Request module for wrapping all request related information., (*5)

The module essentially wraps access to $_GET, $_POST, $_SERVER, information send through the HTTP body and calculates url details (current, site, base, route path) from the request uri., (*6)

The module provides a framework unified way to access these global variables and it provides a way to inject a certain setup when using the module for unit testing., (*7)

Table of content

Class, interface, trait

The following classes, interfaces and traits exists., (*8)

Class, interface, trait Description
Anax\Request\Request Wrapper class for request details and related.

Exceptions

Module specific exceptions are thrown through Anax\Request\Exception., (*9)

Configuration file

There is no configuration file for this module., (*10)

DI service

The module is created as a framework service within $di. You can see the details in the configuration file config/di/request.php., (*11)

It can look like this., (*12)

/**
 * Configuration file for request service.
 */
return [
    // Services to add to the container.
    "services" => [
        "request" => [
            "shared" => true,
            "callback" => function () {
                $obj = new \Anax\Request\Request();
                $obj->init();
                return $obj;
            }
        ],
    ],
];
  1. The object is created as a shared resource.
  2. The init-method reads information from the environment to find out the url of the request.

The service is lazy loaded and not created until it is used., (*13)

General usage within the Anax framework

The request service is a mandatory service within the Anax framework and it is the first service used when handling a request., (*14)

Here is the general flow for receiving a request, mapping it to a route and returning a response. This is found in the frontcontroller htdocs/index.php of an Anax installation., (*15)

// Leave to router to match incoming request to routes
$response = $di->get("router")->handle(
    $di->get("request")->getRoute(),
    $di->get("request")->getMethod()
);
// Send the HTTP response with headers and body
$di->get("response")->send($response);

The request is used to get the request method and the route path, these are used by the router service to find a callback for the route. Each callback can then return a response which is sent through the response service., (*16)

Access as framework service

You can access the module as a framework service., (*17)

# $app style
$app->request->getRoute();

# $di style, two alternatives
$di->get("request")->getRoute();

$request = $di->get("request");
$request->getRoute();

Create and init an object

This is how the object can be created. This is usually done within the framework as a sevice in $di., (*18)

# Create an object
$request = new \Anax\Request\Request();

# Set (reset) globals, useful in unit testing
# when not using $_GET, $_POST, $_SERVER
$request->setGlobals();

# Init the class by extracting url parts and
# route path.
$request->init();

Extract url and route parts

When the object is initiated you can extract url and route parts from it. This is based on the current url., (*19)

# Get site url including scheme, host and port.
$request->getSiteUrl();

# Get base url including site url and path to current index.php.
$request->getBaseUrl();

# Get current url as base url and attach
# the query string.
$request->getCurrentUrl();

# Get script name, index.php or other.
$request->getScriptName();

# Get HTTP request method, for example
# GET, POST, PUT, DELETE.
$request->getMethod();

# Get route path as a string.
$request->getRoute();

# Get route path parts in an array.
$request->getRouteParts();

Get and set $_SERVER

You can get and set values in the PHP global variable $_SERVER., (*20)

# Read a value
$value = $request->getServer($key);

# Read all values as an key value array
$array = $request->getServer();

# Read a value and use $default if $key is not set.
$value = $request->getServer($key, $default);

# Set a value
$request->setServer($key, $value);

You are reading and setting values in a copy of $_SERVER, so you are not actually editing the global variable, just the internal representation within the class., (*21)

Get and set $_GET

You can get and set values in the PHP global variable $_GET., (*22)

# Read a value
$value = $request->getGet($key);

# Read all values as an key value array
$array = $request->getGet();

# Read a value and use $default if $key is not set.
$value = $request->getGet($key, $default);

# Set a value
$request->setGet($key, $value);

You are reading and setting values in a copy of $_GET, so you are not actually editing the global variable, just the internal representation within the class., (*23)

Get and set $_POST

You can get and set values in the PHP global variable $_POST., (*24)

# Read a value
$value = $request->getPost($key);

# Read all values as an key value array
$array = $request->getGet();

# Read a value and use $default if $key is not set.
$value = $request->getPost($key, $default);

# Set a value
$request->setPost($key, $value);

You are reading and setting values in a copy of $_POST, so you are not actually editing the global variable, just the internal representation within the class., (*25)

Get and set request body

You can get and set the value in the HTTP request body. Sometimes the HTTP request body is used to send parameters to an route., (*26)

# Read the body
$request->getBody();

# Read the body and treat it as json
$request->getBodyAsJson()

# Set the body
$request->setBody($content);

You are setting values in a copy of the actual body, so you are not actually editing it, just the internal representation within the class., (*27)

License

This software carries a MIT license. See LICENSE.txt for details., (*28)

 .  
..:  Copyright (c) 2013 - 2020 Mikael Roos, mos@dbwebb.se

The Versions

30/04 2018

dev-master

9999999-dev https://dbwebb.se/anax

Anax Request class.

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

  • php >=5.6

micro framework boilerplate mvc education

25/04 2018

v1.1.1

1.1.1.0 https://dbwebb.se/anax

Anax Request class.

  Sources   Download

MIT

The Requires

  • php >=7.0

 

micro framework boilerplate mvc education

23/04 2018

v1.1.0

1.1.0.0 https://dbwebb.se/anax

Anax Request class.

  Sources   Download

MIT

The Requires

  • php >=7.0

 

micro framework boilerplate mvc education

15/10 2017

v1.0.6

1.0.6.0 https://dbwebb.se/anax

Anax Request class.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

  • php >=5.6

micro framework boilerplate mvc education

26/06 2017

v1.0.5

1.0.5.0 https://dbwebb.se/anax

Anax Request class.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

  • php >=5.6

micro framework boilerplate mvc education

04/04 2017

v1.0.4

1.0.4.0 https://dbwebb.se/anax

Anax Request class.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

  • php >=5.6

micro framework boilerplate mvc education

30/03 2017

v1.0.3

1.0.3.0 https://dbwebb.se/anax

Anax Request class.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

  • php >=5.6

micro framework boilerplate mvc education

13/03 2017

v1.0.2

1.0.2.0 https://dbwebb.se/anax

Anax Request class.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

  • php >=5.6

micro framework boilerplate mvc education

07/03 2017

v1.0.1

1.0.1.0 https://dbwebb.se/anax

Anax Request class.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

  • php >=5.6

micro framework boilerplate mvc education

03/03 2017

v1.0.0

1.0.0.0 https://dbwebb.se/anax

Anax Request class.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

  • php >=5.6

micro framework boilerplate mvc education