Provides traits that add utility functions to a PSR7 ServerResponse
subclass that make it easier to interact with the URI and input. Inspired by the API of Laravel.
composer install rareloop/psr7-server-request-extension
Create a subclass of a PSR7 compatible ServerRequest
object (e.g. Diactoros) and add the InteractsWithInput
and InteractsWithUri
traits.
<?php
namespace App;
use Rareloop\Psr7ServerRequestExtension\InteractsWithInput;
use Rareloop\Psr7ServerRequestExtension\InteractsWithUri;
use Zend\Diactoros\ServerRequest;
class MyServerRequest extends ServerRequest
{
use InteractsWithInput, InteractsWithUri;
}
$request->path();
$request->url(); // e.g. http://test.com/path
$request->fullUrl(); // e.g. http://test.com/path?foo=bar
$request->query();
$request->query('name');
$request->query('name', 'Jane'); // Defaults to "Jane" if not set
$request->post();
$request->post('name');
$request->post('name', 'Jane'); // Defaults to "Jane" if not set
$request->input();
$request->input('name');
$request->input('name', 'Jane'); // Defaults to "Jane" if not set
if ($request->has('name')) {
// do something
}
if ($request->has(['name', 'age'])) {
// do something if both 'name' and 'age' are present
}