Laravel Redirect to URL using redirect() helper

About Us:

We are from free coder. We are a group of web developers who are passionate about web development. You can learn from us php, laravel, node js, vue js, react js and many other topic on web developement.
we are very happy to help you.
free coder

Today what you are going to learn:

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 and laravel 8. 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 lesson i simple redirect "freecoder.co/tags" URL.

Route:

Route::get('freecoder.co/tags', 'HomeController@tags');

Controller Method:

public function home()

{

return redirect('freecoder.co/tags');

}

Redirect back to previous page

In this lesson 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('freecoder.co/tags', array('as'=> 'freecoder.co.tags', 'uses' => 'HomeController@tags'));

Controller Method:

public function home()

{

return redirect()->route('freecoder.co.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 lesson.

Route:

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

Controller Method:

public function home()

{

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

}

Redirect to Controller Action

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

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 lesson:

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 lesson.

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....

Recent Posts

Related Posts