Redirect to Previous Page in Laravel Without Hidden Fields
Developers often face a specific routing challenge when building multi-step workflows or dynamic forms: how to redirect a user back to the exact page they came from after form submission, without relying on server-side session storage or hidden input fields. In traditional PHP applications using $_SERVER['HTTP_REFERER'], developers frequently encounter issues where the header is empty due to cross-origin requests, security headers like X-Frame-Options, or browser behavior during direct navigation. Laravel 5.2 and later versions provide a more robust mechanism for this scenario that avoids these pitfalls entirely by leveraging URL fragments and session state.
The core issue stems from how web browsers handle navigation. When you click a link to go from a list view to an edit form, the browser loads the new page but does not automatically store the source URL in a way that is easily accessible via request->url(). That method correctly returns the current URL of the active request. To solve this, we need to capture the destination URL before the redirect occurs and persist it for the duration of the form submission session.
The most reliable approach in Laravel involves using the session to store the intended redirect path. This pattern is often referred to as "flash session storage" or simply storing a temporary redirect path. Here is how you can implement this cleanly:
Capturing the Destination URL
On the list page where the user initiates the transition to the form, you must explicitly set the destination in the session before rendering the view or performing any redirects. You can do this by accessing the session facade or injecting it into your controller.
use Illuminate\Support\Facades\Session;
public function showForm($id)
{
Session::put('redirect_url', request()->url());
return view('forms.edit', compact('id'));
}
In this example, request()->url() captures the current URL (the form page), but waitâthe logic needs to be inverted based on the user's flow. If the user is currently on the list and clicks into the form, the list page URL is what we want to redirect back to after submission. Therefore, you should capture the request()->fullUrl() on the list page before passing data to the form view, or better yet, set it immediately upon entering the list route:
public function listIndex()
{
// Capture the current URL which is the destination for the redirect
Session::put('redirect_url', request()->url());
return view('projects.index');
}
This ensures that whenever a user arrives at this specific list page (whether from Project A or Client B), the system knows exactly where they came from.
Handling the Form Submission
When your form is submitted, you simply retrieve this stored value and pass it to the redirect helper. Laravel's redirect() function accepts a string URL, allowing you to seamlessly send the user back to their original list view.
public function store(Request $request)
{
// Validate logic here...
// Retrieve the stored destination and redirect there
return redirect()->intended(Session::get('redirect_url'));
}
The intended method is a built-in Laravel helper designed specifically for this purpose. It attempts to redirect to a URL stored in the session under the key url. If no such URL exists, it falls back to the default redirect location (usually the current page). This makes your code resilient even if the session data is missing or cleared unexpectedly.
Trade-offs and Considerations
While this method is superior to relying on $_SERVER['HTTP_REFERER'], it is important to understand its limitations. Since the redirect path is stored in the session, the user must remain logged in for the duration of their session. If a user logs out and then attempts to submit the form, the session will be invalidated, and the redirect_url key will not exist. In such cases, Laravel's intended method gracefully defaults to the current page or your application's home route, preventing a broken navigation experience.
Another consideration is security. You should ensure that the redirect_url points only to trusted domains within your application. While storing a URL in the session is generally safe because it requires valid session credentials to read, you might want to validate the scheme and host of the stored URL before redirecting if your application has strict CSP (Content Security Policy) requirements.
For more advanced scenarios involving complex state management or multi-step wizards, libraries like laravel-flash or packages provided by https://laravelcompany.com can offer additional utilities to manage flash messages and redirects with even less boilerplate code. However, the native session-based approach described here remains the standard for lightweight, dependency-free solutions in Laravel 5.2+.
