Laravel Redirect to Route from Controller - TechvBlogs

Laravel Redirect to Route from Controller

In this article, You will learn How to Redirect to Route from Laravel Controller.


Smit Pipaliya - Author - TechvBlogs
Smit Pipaliya
 

1 month ago

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 Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
use App\Http\Controllers\HomeController;

/*
|--------------------------------------------------------------------------
| 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()

App\Http\Controller\UserController:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    public function index()
    {
        $users = User::get();

        return redirect()->route("home");
    }
}

Using to_route()

App\Http\Controller\UserController:

<?php
    
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
use App\Models\User;
    
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $users = User::get();
  
        return to_route("home");
    }
}

Using redirect()

App\Http\Controller\UserController:

<?php
    
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
use App\Models\User;
    
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    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 App\Http\Controllers;
    
use Illuminate\Http\Request;
use App\Models\User;
    
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    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 App\Http\Controllers;
    
use Illuminate\Http\Request;
use App\Models\User;
    
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    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 App\Http\Controllers;
    
use Illuminate\Http\Request;
use App\Models\User;
    
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    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.

Read Also: How to Convert JSON to Array in Laravel

If you want to manage your VPS / VM Server without touching the command line go and  Checkout this linkServerAvatar allows you to quickly set up WordPress or Custom PHP websites on VPS / VM in a  matter of minutes.  You can host multiple websites on a single VPS / VM, configure SSL certificates, and monitor the health of your server without ever touching the command line interface.

If you have any queries or doubts about this topic please feel free to contact us. We will try to reach you.

Comments (0)

ServerAvatar
Multiple sites on a single VPS/VM/Server Deploy PHP sites on cloud in minutes, simple as that!
ServerAvatar
Generate Free SSL Certificate - SSLForWeb

Generate Free SSL Certificate

  • 90-Day Certificates
  • SSL Monitoring
  • Wildcard Certificates
  • Multiple Domains
  • Certificate Management
  • Expiration Reminders

Comment


Note: All Input Fields are required.