Laravel Redirect to Route from Controller
In this post we will give you information about Laravel Redirect to Route from Controller. Hear we will give you detail about Laravel Redirect to Route from Controller And how to use it also give you demo for it if it is necessary.
This article will teach us Laravel redirect from controller to route. We will see how to laravel redirects from the route name. I will explain through a simple example how to redirect the route in laravel controller.
You mostly need to redirect the route from the controller method in the laravel project. Laravel provides several ways to return redirect with route name in laravel. Here, you will see four ways to return a redirect to a specific route from the controller function.
Example Routes:
routes/web.php
<?php
use IlluminateSupportFacadesRoute;
use AppHttpControllersUserController;
use AppHttpControllersHomeController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('users', [UserController::class, 'index']);
Route::get('home', [HomeController::class, 'index'])->name("home");
Using redirect
() with route
()
AppHttpControllerUserController:
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppModelsUser;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return IlluminateHttpResponse
*/
public function index()
{
$users = User::get();
return redirect()->route("home");
}
}
Using to_route
()
AppHttpControllerUserController:
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppModelsUser;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return IlluminateHttpResponse
*/
public function index()
{
$users = User::get();
return to_route("home");
}
}
Using redirect
()
AppHttpControllerUserController:
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppModelsUser;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return IlluminateHttpResponse
*/
public function index()
{
$users = User::get();
return redirect("home");
}
}
Using route
() with Parameters
Here’s an example if you can pass the route parameters as the second argument to route()
:
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppModelsUser;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return IlluminateHttpResponse
*/
public function show(User $user)
{
return Redirect::route('home', [$user->id])->with('message', 'User id found.');
}
}
If it’s only one you also don’t need to write it as an array:
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppModelsUser;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return IlluminateHttpResponse
*/
public function show(User $user)
{
return Redirect::route('home', $user->id)->with('message', 'User id found.');
}
}
In case your route has more parameters, or if it has only one, but you want to clearly specify which parameter has each value (for readability purposes), you can always do this:
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppModelsUser;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return IlluminateHttpResponse
*/
public function show(User $user)
{
return Redirect::route('home', ['id'=>$user->id,'OTHER_PARAM'=>'XXX',...])->with('message', 'User id found.');
}
}
Thank you for reading this article.
Also see: How to Convert JSON to Array in Laravel
. .
If you have any queries or doubts about this topic please feel free to contact us. We will try to reach you.
Hope this code and post will helped you for implement Laravel Redirect to Route from Controller. 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