Every route or controller action must return a response to the user’s browser. Laravel makes it easy to return strings, arrays, views, redirects, and JSON — all with minimal code.
The redirect() helper returns a redirect response. It’s commonly used after form submissions to send users to a different page:
use Illuminate\Http\RedirectResponse;Route::get('/old-page', function (): RedirectResponse { return redirect('/new-page');});
Controller example:
<?phpnamespace App\Http\Controllers;use Illuminate\Http\RedirectResponse;use Illuminate\Http\Request;class PostController extends Controller{ public function store(Request $request): RedirectResponse { // Save the post... return redirect('/posts'); }}
Use the route() helper to redirect by route name instead of a hard-coded URL. If the URL ever changes, you only need to update the route definition:
// Defining named routesRoute::get('/posts', [PostController::class, 'index'])->name('posts.index');Route::get('/posts/{post}', [PostController::class, 'show'])->name('posts.show');
// Redirect to a named routereturn redirect()->route('posts.index');// With route parametersreturn redirect()->route('posts.show', ['post' => $post->id]);
<?phpnamespace App\Http\Controllers;use App\Models\Post;use Illuminate\Http\RedirectResponse;use Illuminate\Http\Request;use Illuminate\View\View;class PostController extends Controller{ // Return a view for listing posts public function index(): View { $posts = Post::latest()->get(); return view('posts.index', ['posts' => $posts]); } // Return a view for the creation form public function create(): View { return view('posts.create'); } // Save and redirect public function store(Request $request): RedirectResponse { $post = Post::create($request->validated()); return redirect() ->route('posts.show', ['post' => $post->id]) ->with('success', 'Post created successfully.'); } // Return a view for a single post public function show(Post $post): View { return view('posts.show', ['post' => $post]); } // Delete and redirect public function destroy(Post $post): RedirectResponse { $post->delete(); return redirect() ->route('posts.index') ->with('success', 'Post deleted.'); }}
In web applications, display actions return a View and mutating actions (create, update, delete) redirect after completing. This pattern prevents duplicate form submissions on refresh.