Laravel Redirect to URL using redirect() helper

Laravel Redirect to URL using redirect() helper

In this post we will give you information about Laravel Redirect to URL using redirect() helper. Hear we will give you detail about Laravel Redirect to URL using redirect() helperAnd how to use it also give you demo for it if it is necessary.

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.0, Laravel 5.1, Laravel 5.2 and Laravel 5.3. Laravel 5 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 “onlinecode/tags” URL.

Route:

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

Controller Method:

public function home()

{

return redirect('onlinecode/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('onlinecode/tags', array('as'=> 'onlinecode.tags', 'uses' => 'HomeController@tags'));

Controller Method:

public function home()

{

return redirect()->route('onlinecode.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('onlinecode/tag/{id}', array('as'=> 'onlinecode.tag', 'uses' => 'HomeController@tags'));

Controller Method:

public function home()

{

return redirect()->route('onlinecode.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('AppHttpControllersHomeController@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('AppHttpControllersHomeController@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.

Also see:Laravel – Multiple Files Download with Response 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….

Hope this code and post will helped you for implement Laravel Redirect to URL using redirect() helper. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  16  =  25

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US