expand_less

Requests

Introduction

Dreamfork's Framework\Http\Request class offers an object-oriented approach to engage with the current HTTP request being processed by your application. It also allows you to retrieve input, cookies, and files submitted with the request.

Interacting With The Request

To obtain the current request instance within a controller, you can use type-hinting for the Framework\Http\Request class on your route closure or controller method. As mentioned in the Routing section, the framework automatically passes the global parameter $request, providing convenient access to the request instance.

                                
                                    
<?php
 
namespace App\Controllers;
 
use Framework\Http\Request;
 
class UserController extends Controller
{
public function store(Request $request)
{
return $request->input('name');
}
}

Dependency Injection & Route Parameters

If your controller method is also expecting input from a route parameter, you should list your route parameters after your other dependencies. For example, if your route is defined like this:

                                
                                    
Route::put('/user/{id}', [UserController::class, 'update']);

You can still type-hint the Framework\Http\Request and access your id route parameter by defining your controller method as follows:

                                
                                    
<?php
 
namespace App\Controllers;
 
use Framework\Http\Request;
 
class UserController extends Controller
{
public function update(Request $request, string $id)
{
// ...
}
}

It's important to note that the order of arguments in the method is not crucial. In other words, you can use public function update(string $id, Request $request). The key aspect is to ensure that the argument names align with the route parameters for proper binding.

Request Path, Host, & Method

The Framework\Http\Request instance provides various methods for inspecting the incoming HTTP request and extends the Symfony\Component\HttpFoundation\Request class. Here, we'll explore some of the key methods.

Retrieving The Request Path

The getPathInfo() method returns the request's path information. For instance, if the incoming request is directed at http://example.com/foo/bar, calling $request->getPathInfo() will return foo/bar:

                                
                                    
$uri = $request->getPathInfo();

Retrieving The Request Method

Use the isMethod method to verify that the HTTP verb matches a given string:

                                
                                    
if ($request->isMethod('post')) {
// ...
}

Request Headers

You can retrieve a request header from the Framework\Http\Request instance using the header method. If the header is not present on the request, null will be returned. However, the header method accepts an optional second argument that will be returned if the header is not present:

                                
                                    
$value = $request->header('X-Header-Name');
$value = $request->header('X-Header-Name', 'default');

For convenience, the bearerToken method may be used to retrieve a bearer token from the Authorization header. If no such header is present, an empty string will be returned:

                                
                                    
$token = $request->bearerToken();

Input

Retrieving Input

You can obtain all incoming request input data as an array using the all method. This approach works whether the incoming request is from an HTML form or an XHR request:

                                
                                    
$input = $request->all();

Retrieving An Input Value

Regardless of the HTTP verb used for the request, you can access all user input from your Framework\Http\Request instance using the input method:

                                
                                    
$name = $request->input('name');

You can pass a default value as the second argument to the input method, which will be returned if the requested input value is not present:

                                
                                    
$name = $request->input('name', 'John');

Calling the input method without any arguments retrieves all input values as an array, just like all method:

                                
                                    
$input = $request->input();

Retrieving Input Via Dynamic Properties

User input can also be accessed using dynamic properties on the Framework\Http\Request instance. For instance, if one of your application's forms includes a name field, you can access the value like this:

                                
                                    
$name = $request->name;