anax / response
Anax Response module, to create a HTTP response.
Installs: 18 908
Dependents: 38
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 2
Open Issues: 1
Requires
- php: ^7.2
Requires (Dev)
- anax/commons: ^2.0.0@alpha
- anax/configure: ^2.0.0@alpha
- anax/di: ^2.0.0@alpha
- phpunit/phpunit: ^7
This package is auto-updated.
Last update: 2024-09-27 21:33:15 UTC
README
Anax Response module to send HTTP responses.
The module is used to send a HTTP response from the Anax framework, including status code, headers, and body.
Table of content
- Class, interface, trait
- Exceptions
- Configuration file
- DI service
- General usage within the Anax framework
- Access as framework service
- Create, init and use an object
Class, interface, trait
The following classes, interfaces and traits exists.
Exceptions
Module specific exceptions are thrown through Anax\Response\Exception
.
Configuration file
There is no configuration file for this module.
DI service
The module is created as a framework service within $di
. You can see the details in the configuration file config/di/response.php
.
It can look like this.
/** * Configuration file for DI container. */ return [ // Services to add to the container. "services" => [ "response" => [ "shared" => true, //"callback" => "\Anax\Response\Response", "callback" => function () { $obj = new \Anax\Response\ResponseUtility(); $obj->setDI($this); return $obj; } ], ], ];
- The object is created as a shared resource.
- DI is injected into the module, when using ResponseUtility.
The service is lazy loaded and not created until it is used.
General usage within the Anax framework
The response service is a mandatory service within the Anax framework and it is the first service used when handling a request to the framework.
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.
// 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.
Access as framework service
You can access the module as a framework service.
# $app style $app->response->redirectSelf(); # $di style, two alternatives $di->get("response")->redirectSelf(); $response = $di->get("response"); $response->redirectSelf();
Create, init and use an object
This is how the object can be created. This is usually done within the framework as a sevice in $di
.
# Create an object response = new \Anax\Response\Response(); # Send a response response->send("Some response"); response->sendJson(["key" => "Some value"]); response->redirect($absoluteUrl);
The added benefit of using ResponseUtility is that this class is injected with DI and makes it easier to use the redirect methods for urls within the framework.
# Create an object response = new \Anax\Response\ResponseUtility(); # Do a redirect response->redirect("game/init"); response->redirectSelf();
License
This software carries a MIT license. See LICENSE.txt for details.
.
..: Copyright (c) 2013 - 2019 Mikael Roos, mos@dbwebb.se