How to Redirect to URL using redirect() helper in Laravel?

Today now in this tutorial, i will be show you how we can redirect any user one page to another page from the controller method. We are normally use the redirect() helper method for the redirect user in controller.

We can simply do this by using the redirect() helper in laravel 5, laravel 6, laravel 7 and laravel 8 any version. Laravel is provided us redirect(). So there are the several way to do redirect URL in Laravel application. Now in this post i am going to give you a way to redirect URL also with the parameters.

There are the several methods through we can redirect URL in Laravel application 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

1. Redirect to URL

We can simply redirect by given URL, bellow example i will simple redirect “codingspoint/tags” URL.

Route:

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

Controller Method:

public function home()
{
    return redirect('codingspoint/tags');
}
2. Redirect back to previous page

Now in this example, we can see the redirect back to our previous page URL, so we can do it on both way:

public function home()
{
    return back();
}
OR
public function homenew()
{
    return redirect()->back();
}
3. Redirect to Named Routes

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

Route:

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

Controller Method:

public function home()
{
    return redirect()->route('codingspoint.tags');
}
4.Redirect to Named Routes with parameters

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

Route:

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

Controller Method:

public function home()
{
    return redirect()->route('itsolutionstuff.tag',['id'=>17]);
}
5.Redirect to Controller Action

we can also redirect from the controller method by using action of redirect() helper as we can see in the bellow example:

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

Redirect to Controller Action With Parameters

So we can also redirect from the controller method by using action also with parameters of redirect() helper as we can see in the bellow example:

public function home()
{
    return redirect()->action('App\Http\Controllers\HomeController@home',['id'=>17]);
}
6. Redirect with Flashed Session Data

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

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

Read Also: How to implement infinite ajax scroll pagination in Laravel?

Thanks for read. I hope it help you. For more you can follow us on facebook

Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.