HTTP Request
- Introduction
- Accessing the request
- Retrieving data
- Request path and method
- Retrieving the request path
- Retrieving the request method
- Checking whether the request is an AJAX request
- Retrieving an input
- Retrieving a subset of input data
- Determining whether an input value is present
- Determining the client IP address
- Determining the client port
- Determining the client protocol
- Retrieving the HTTP headers
- Retrieving the server IP
- Retrieving the Origin domain
- Retrieving the request time
- Retrieving a session instance
- Retrieving the current authentication
Introductionβ
An HTTP request is the message that the user sends to the Bow application through their browser or another tool such as curl, httpie, and the like.
This message is handled by Bow through the Bow\Http\Request class. This class allows you to get information about the request sent by the user, such as their IP address, the type of message, the data from a form, and so on.
Accessing the requestβ
To capture an HTTP message or request, you can first use the request() helper, which gives you an instance of Bow\Http\Request; second, you can use dependency injection through a controller action.
Retrieving dataβ
Using a few simple methods, you can access all of your application's user input.
Via route Closuresβ
- With the
requesthelper:
$app->get('/', function ()
{
$request = request();
$name = $request->get('name');
// code here
});
- Through dependency injection
use Bow\Http\Request;
$app->get('/', function (Request $request)
{
return $request->get('name');
});
Via a controllerβ
If your controller method also expects input from a route parameter, you must list your route parameters after your other dependencies. For example, if your route is defined as follows:
$app->get('/users/:id', 'UserController::show');
You can still type-hint the Bow\Http\Request request and access the id from your route parameter by defining your controller method as follows:
namespace App\Controllers;
use App\Controllers\Controller;
use App\Models\User;
use Bow\Http\Request;
class UserController extends Controller
{
/**
* Display the profile for the given user
*
* @param Request $reuest
* @return mixed
*/
public function show(Request $reuest, $id)
{
$user = User::where('id', $id)->first();
$user->name = $request->get('name');
$user->save();
return $this->render('user/profile', ['user' => $user]);
}
}
Request path and methodβ
Retrieving the request pathβ
The path method returns the path information of the request. So, if the incoming request targets http://example.com/bar/zar, the path method will return bar/zar:
$uri = $request->path();
The is method allows you to check that the incoming request path matches a given pattern. You can use the * character as a wildcard when using this method:
if ($request->is('users/*')) {
//
}
Retrieving the request methodβ
The method method will return the HTTP verb for the request. You can use the isPost, isGet, isPut, isDelete, isOptions, isPutch methods to check that the HTTP verb matches a given request's HTTP method:
$method = $request->method();
if ($request->isPost()) {
$filename = $request->file('filename');
$filename->moveTo('/some/directory');
}
Checking whether the request is an AJAX requestβ
To determine whether a request was sent via AJAX, the isAjax method lets you check this.
if ($request->isAjax()) {
//
}
Retrieving an inputβ
Often the request sent by the HTTP client contains data that is needed to trigger a mechanism, such as a registration.
During its startup, BowPHP optimizes the data sent to the server by replacing empty values with null and removing whitespace from each field's values using the PHP trim function.
$name = $request->get('name');
// Retrieve the value directly via the field name
$name = $request->name;
You can also set a default value for
get
$name = $request->get('name', 'Papac');
$name = $request->get('name', function () {
return User::first()->name;
});
This makes it possible to retrieve the contents of a form's name field or a URL's, depending on the type of request.
Retrieving a subset of input dataβ
If you need to extract a subset of the input data, you can use the ignore and only methods. Both methods accept either a single array or a dynamic list of arguments:
$input = $request->only(['name', 'lastname']);
$input = $request->only('name', 'lastname');
$input = $request->ignore(['password']);
$input = $request->ignore('password');
Determining whether an input value is presentβ
You should use the has method to determine whether a value is present in the request. The has method returns true if the value is present in the request:
if ($request->has('name')) {
//
}
Determining the client IP addressβ
Often you will want the IP address of the client that sent the request; the ip method, as its name suggests, retrieves the client's IP address:
$ip = $request->ip();
if ($ip == 'xxx.xxx.xxx.xxx') {
// code here
}
Determining the client portβ
The port method, as its name suggests, retrieves the client's port:
$port = $request->port();
// Code here
Determining the client protocolβ
The protocol method, as its name suggests, retrieves the client's protocol:
$protocol = $request->protocol();
// Code here
There is also the isProtocol method, which allows you to check the protocol:
if ($request->isProtocol('http')) {
//
}
And isSecure lets you check whether the request is secure.
if ($request->isSecure()) {
//
}
Retrieving the HTTP headersβ
You can retrieve the client's HTTP headers with the getHeader method and use hasHeader to check for the existence of an HTTP header.
$header = $request->getHeader('content-type');
echo $header;
// text/html
if ($request->hasHeader('x-proxy-key')) {
// code here
}
Retrieving the server IPβ
You can retrieve the IP address of the server on which your Bow application is hosted.
$hostname = $request->hostname();
echo $hostname;
// exemple.com
Retrieving the Origin domainβ
The so-called origin domain is the address retrieved with the hostname method, combined with the request's protocol.
$hostname = $request->origin();
echo $hostname;
// https://exemple.com
Retrieving the request timeβ
It is often useful to see how long a request took. The time method lets us do this and returns a timestamp.
$time = $request->time();
echo $time;
Retrieving a session instanceβ
It is often useful to manipulate the session directly. The session method lets us do this and returns the session instance.
$session = $request->session();
var_dump($session);
For more information about the session, visit this page HTTP Session
Retrieving the current authenticationβ
You can also get the current authentication if you have used BowPHP's native authentication system.
$user = $request->user();
// Define the guard
$user = $request->user('api');
Is something missing?
If you run into problems with the documentation or have suggestions to improve the documentation or the project in general, please open an issue for us, or send a tweet mentioning the Twitter account @bowframework or directly on github.