Laravel Redirect to URL using redirect() helper

By Hardik Savani February 18, 2023 Category : Laravel

In this tutorial, i am going to tell you how to redirect user one page to another page from controller method. we normally use

redirect() helper method for redirect user in controller.

We can simply use redirect() helper in laravel 5, laravel 6, laravel 7, laravel 8, laravel 9 and laravel 10. Laravel version provided redirect(). there are several way to do redirect URL in Laravel. In this post i am going to give you all the way to redirect URL with parameters.

There are several methods through we can redirect URL in Laravel 5 as listed bellow:

1) Redirect to URL

2) Redirect back to previous page

3) Redirect to Named Routes

4) Redirect to Named Routes with parameters

5) Redirect to Controller Action

6) Redirect to Controller Action With Parameters

7) Redirect with Flashed Session Data

Redirect to URL

you can simply redirect given URL, bellow example i simple redirect "itsolutionstuff/tags" URL.

Route:

Route::get('itsolutionstuff/tags', 'HomeController@tags');

Controller Method:

public function home()

{

return redirect('itsolutionstuff/tags');

}

Redirect back to previous page

In this example, we can redirect back to our previous page URL, so you can do it both way:

public function home()

{

return back();

}

OR

public function home2()

{

return redirect()->back();

}

Redirect to Named Routes

If you declare route with name and you want to redirect route from controller method then you can simply do it.

Route:

Route::get('itsolutionstuff/tags', array('as'=> 'itsolutionstuff.tags', 'uses' => 'HomeController@tags'));

Controller Method:

public function home()

{

return redirect()->route('itsolutionstuff.tags');

}

Redirect to Named Routes with parameters

If you declare route with name and also parameters and you want to redirect route from controller method then you can do it by following example.

Route:

Route::get('itsolutionstuff/tag/{id}', array('as'=> 'itsolutionstuff.tag', 'uses' => 'HomeController@tags'));

Controller Method:

public function home()

{

return redirect()->route('itsolutionstuff.tag',['id'=>17]);

}

Redirect to Controller Action

we can also redirect controller method using action of redirect() helper as you can see bellow example:

public function home()

{

return redirect()->action('App\Http\Controllers\HomeController@home');

}

Redirect to Controller Action With Parameters

we can also redirect controller method using action with parameters of redirect() helper as you can see bellow example:

public function home()

{

return redirect()->action('App\Http\Controllers\HomeController@home',['id'=>17]);

}

Redirect with Flashed Session Data

we can also pass flashed session message while redirect with routes or url in controller method as you can see bellow example.

public function home()

{

return redirect('home')->with('message', 'Welcome to ItSolutionStuff Tutorials!');

}

You can simply redirect above ways to URL from controller method.

Maybe it can help you....