Skip to content

Request Class ​

The Request class represents the current HTTP request.

Properties ​

PropertyTypeDescription
methodstringHTTP method (GET, POST, etc.)
pathstringRequest path
queryarrayGET parameters
bodyarrayPOST parameters
jsonarrayJSON request body
headersarrayRequest headers
cookiesarrayCookie values
filesarrayUploaded files
ipstringClient IP address

Methods ​

Parameter Access ​

php
$id = $request->param('id');           // Route parameter
$email = $request->input('email');      // POST/GET/JSON input
$name = $request->input('name', 'default');
$all = $request->all();                  // All input
$only = $request->only(['name', 'email']);
$except = $request->except(['password']);

Checks ​

php
if ($request->has('email')) { ... }
if ($request->filled('email')) { ... }
if ($request->missing('email')) { ... }
if ($request->isJson()) { ... }
if ($request->wantsJson()) { ... }
if ($request->isMethod('POST')) { ... }

Authentication ​

php
$token = $request->bearerToken();
$user = $request->user();
if ($request->isAuthenticated()) { ... }

CSRF Protection ​

php
if (!$request->validateCsrf()) {
    return Response::error('Invalid CSRF token', 419);
}

Session ​

php
$value = $request->session('key');
$all = $request->session();

Helpers ​

php
$ip = $request->getClientIp();
$ua = $request->getUserAgent();
$url = $request->isSecure();
$path = $request->path;

Attributes (Middleware Data Passing) ​

Middleware can attach arbitrary data to the request for downstream handlers:

php
// In middleware
Flow::use(function($req) {
    $user = Auth::find($req->bearerToken());
    $req->setAttribute('user', $user);
    return null;
});

// In route handler
Flow::GET()->do(function($req) {
    $user = $req->getAttribute('user');
    return Response::json(['name' => $user->name]);
});
php
$request->setAttribute('key', $value);
$request->getAttribute('key', $default = null);

Released under the MIT License.