Securing PHP APIs with Laravel Passport
When a PHP API serves multiple clients, Laravel Passport lets those clients authenticate and receive limited permissions through OAuth 2.0. OAuth 2.0…
After a Laravel controller handles a request, it can return a redirect response that tells the browser to request another URL. The response includes an HTTP status code and a Location header, which tells the browser where to make its next request. The browser then requests that destination; Laravel does not internally jump to another controller action.
Use redirect() to send a browser to a URL, or target a named route when you want Laravel to build the destination from your route definitions. For a form submission, Laravel can save the data, redirect the browser to a result page, and pass a short-lived status message to that page.
To redirect to a path, pass it to Laravel’s redirect() helper:
return redirect('/dashboard');
Laravel returns a redirect response with a 302 status by default, as described in its helper documentation. The browser follows the response’s Location header and requests the destination.
For an external destination, Laravel provides away():
return redirect()->away('https://example.com');
Laravel does not validate or verify the URL passed to away(). If the destination comes from a request parameter or other user-controlled value, validate it before redirecting so your code does not send users to an unintended site. See Laravel’s redirect response documentation.
A named route gives a route a stable name that code can use instead of repeating its path. Define the name with name():
use App\Http\Controllers\PostController;
use Illuminate\Support\Facades\Route;
Route::get('/posts/{post}', [PostController::class, 'show'])
->name('posts.show');
Then redirect to it with route() or the to_route() helper:
return redirect()->route('posts.show', ['post' => $post->id]);
// Equivalent helper:
return to_route('posts.show', ['post' => $post->id]);
Laravel fills the {post} segment using the post parameter. If you pass additional key-value pairs that do not match route parameters, Laravel adds them to the query string. You can also pass an Eloquent model for a route parameter; Laravel uses the model’s route key to build the URL. The named route documentation covers route names and parameters. For more on how route names help build URLs, see generating URLs with named routes.
A typical browser form flow sends a POST request to Laravel. The controller validates the submitted data and saves it, then returns a redirect response. The browser follows that response with a separate request to the destination page.
For example, a controller can create a post and redirect to its detail page:
public function store(Request $request)
{
$validated = $request->validate([
'title' => ['required', 'max:255'],
]);
$post = Post::create($validated);
return to_route('posts.show', ['post' => $post->id])
->with('status', 'Post created.');
}
The with() call stores a message in the session as flash data, which is available during the next request. The destination page can then display it once. The redirect chooses the next URL; the flashed message separately carries data to that page. Laravel documents this behavior under session flash data.
With the default 302 response, browsers typically follow a form POST with a GET request. HTTP defines redirect statuses with different method behavior: a 303 directs the client to retrieve another resource, while a 307 preserves the original request method when the client follows the redirect. The HTTP specification also describes historical ambiguity around whether a client changes the method after a 302. Treat POST-then-GET as the usual browser form flow, rather than a guarantee for every client and status code.
For a traditional form submission, Laravel redirects back when validation fails and flashes the validation errors and submitted input to the session. The form can then show the errors and repopulate fields. For a browser request made with XMLHttpRequest (XHR), Laravel returns a JSON response with a 422 status instead of using the traditional redirect flow. Laravel describes these paths in its validation documentation.
You can also redirect back manually and flash input. Exclude sensitive values such as passwords before storing the input in the session:
return back()->withInput(
$request->except('password')
);
The back() helper relies on session data, so the route should use Laravel’s web middleware group. The request documentation explains how to flash input and exclude fields.
Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.
Take a look at vroni.com