Dispatch the request to the application.
public dispatch ( Illuminate\Http\Request $request ) : Illuminate\Http\Response | ||
$request | Illuminate\Http\Request | |
return | Illuminate\Http\Response |
// Define a simple route $router->get('/hello', function () { return 'Hello World!'; }); // Dispatch the request to the matching controller $response = $router->dispatch(Request::create('/hello', 'GET')); echo $response->getContent(); // Output: Hello World!
// Define a route with parameter $router->get('/hello/{name}', function ($name) { return "Hello $name!"; }); // Dispatch the request to the matching controller $response = $router->dispatch(Request::create('/hello/Jane', 'GET')); echo $response->getContent(); // Output: Hello Jane!
// Define a route with controller action $router->get('/user/{id}', 'UserController@show'); // Dispatch the request to the matching controller $response = $router->dispatch(Request::create('/user/123', 'GET')); echo $response->getContent(); // Output: User with ID 123In this example, we define a route that maps to a controller action 'UserController@show'. The UserController class should exists and have the method show($id) to handle the request. The $id parameter value will be captured from the URI and passed to the method. We then dispatch the request, execute the controller action and get the response object. Overall, the `Illuminate\Routing\Router` class provides a powerful and flexible way to define and dispatch routes for your Laravel application. The package library that it belongs to is the Laravel Routing package.
public dispatch ( Illuminate\Http\Request $request ) : Illuminate\Http\Response | ||
$request | Illuminate\Http\Request | |
return | Illuminate\Http\Response |