Laravel Redirect to URL using redirect() Helper

By Hardik Savani April 16, 2024 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, laravel 10 and laravel 11. 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) Laravel Redirect to URL

2) Laravel Redirect back to previous page

3) Laravel Redirect to Named Routes

4) Laravel Redirect to Named Routes with parameters

5) Laravel Redirect to Controller Action

6) Laravel Redirect to Controller Action With Parameters

7) Laravel Redirect with Flashed Session Data

Laravel Controller Redirect to URL

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

Route:

Route::get('users', 'UserController@tags');

Controller Method:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

return redirect('/users');

}

}

Laravel Controller Redirect Back to Previous Page

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

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

/* return redirect()->back(); */

return back();

}

}

Laravel Controller 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('users', [UserController::class, 'show'])->name('users.index');

Controller Method:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

return redirect()->route('users.index');

}

}

Laravel Controller 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('/users/{id}', [UserController::class, 'show'])->name('users.show');

Controller Method:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

return redirect()->route('users.show', 2);

}

}

Laravel Controller Redirect to Controller Action

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

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

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

}

}

Laravel Controller 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:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

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

}

}

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

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

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

Shares