Skip to main content
Glama
brianirish

Laravel MCP Companion

by brianirish
redirects.md4.48 kB
# HTTP Redirects - [Creating Redirects](#creating-redirects) - [Redirecting To Named Routes](#redirecting-named-routes) - [Redirecting To Controller Actions](#redirecting-controller-actions) - [Redirecting With Flashed Session Data](#redirecting-with-flashed-session-data) <a name="creating-redirects"></a> ## Creating Redirects Redirect responses are instances of the `Illuminate\Http\RedirectResponse` class, and contain the proper headers needed to redirect the user to another URL. There are several ways to generate a `RedirectResponse` instance. The simplest method is to use the global `redirect` helper: ```php Route::get('/dashboard', function () { return redirect('/home/dashboard'); }); ``` Sometimes you may wish to redirect the user to their previous location, such as when a submitted form is invalid. You may do so by using the global `back` helper function. Since this feature utilizes the [session](/docs/{{version}}/session), make sure the route calling the `back` function is using the `web` middleware group or has all of the session middleware applied: ```php Route::post('/user/profile', function () { // Validate the request... return back()->withInput(); }); ``` <a name="redirecting-named-routes"></a> ## Redirecting To Named Routes When you call the `redirect` helper with no parameters, an instance of `Illuminate\Routing\Redirector` is returned, allowing you to call any method on the `Redirector` instance. For example, to generate a `RedirectResponse` to a named route, you may use the `route` method: ```php return redirect()->route('login'); ``` If your route has parameters, you may pass them as the second argument to the `route` method: ```php // For a route with the following URI: profile/{id} return redirect()->route('profile', ['id' => 1]); ``` For convenience, Laravel also offers the global `to_route` function: ```php return to_route('profile', ['id' => 1]); ``` <a name="populating-parameters-via-eloquent-models"></a> #### Populating Parameters Via Eloquent Models If you are redirecting to a route with an "ID" parameter that is being populated from an Eloquent model, you may pass the model itself. The ID will be extracted automatically: ```php // For a route with the following URI: profile/{id} return redirect()->route('profile', [$user]); ``` If you would like to customize the value that is placed in the route parameter, you should override the `getRouteKey` method on your Eloquent model: ```php /** * Get the value of the model's route key. */ public function getRouteKey(): mixed { return $this->slug; } ``` <a name="redirecting-controller-actions"></a> ## Redirecting To Controller Actions You may also generate redirects to [controller actions](/docs/{{version}}/controllers). To do so, pass the controller and action name to the `action` method: ```php use App\Http\Controllers\HomeController; return redirect()->action([HomeController::class, 'index']); ``` If your controller route requires parameters, you may pass them as the second argument to the `action` method: ```php return redirect()->action( [UserController::class, 'profile'], ['id' => 1] ); ``` <a name="redirecting-with-flashed-session-data"></a> ## Redirecting With Flashed Session Data Redirecting to a new URL and [flashing data to the session](/docs/{{version}}/session#flash-data) are usually done at the same time. Typically, this is done after successfully performing an action when you flash a success message to the session. For convenience, you may create a `RedirectResponse` instance and flash data to the session in a single, fluent method chain: ```php Route::post('/user/profile', function () { // Update the user's profile... return redirect('/dashboard')->with('status', 'Profile updated!'); }); ``` You may use the `withInput` method provided by the `RedirectResponse` instance to flash the current request's input data to the session before redirecting the user to a new location. Once the input has been flashed to the session, you may easily [retrieve it](/docs/{{version}}/requests#retrieving-old-input) during the next request: ```php return back()->withInput(); ``` After the user is redirected, you may display the flashed message from the [session](/docs/{{version}}/session). For example, using [Blade syntax](/docs/{{version}}/blade): ```blade @if (session('status')) <div class="alert alert-success"> {{ session('status') }} </div> @endif ```

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/brianirish/laravel-mcp-companion'

If you have feedback or need assistance with the MCP directory API, please join our Discord server