0
votes

How I can redirect the Laravel 7 auth home URL to the dashboard.

My route filE route/web.php

use Illuminate\Support\Facades\Route;    

Auth::routes();
Route::get('/dashboard', 'HomeController@index')->name('dashboard');
Route::get('/', function () {
    return view('/home');
})->middleware('auth');

MyLogin Controller seems like this

namespace App\Http\Controllers\Auth;    
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller
{
    use AuthenticatesUsers;

    protected $redirectTo = RouteServiceProvider::HOME;

    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
}
2

2 Answers

0
votes

So simple solution is :

use Illuminate\Support\Facades\Route;    

Auth::routes();
Route::get('/dashboard', 'HomeController@index')->name('dashboard');
Route::get('/', 'HomeController@index');
0
votes

After spending a few hours on this I found the below solutions

Just Make Changes in app\Providers\RouteServiceProvider.php

public const HOME = '/home';

To

public const DASHBOARD = '/dashboard';

Then make small changes in another files i.e app\Http\Controllers\Auth\LoginController.php

protected $redirectTo = RouteServiceProvider::HOME;

To

protected $redirectTo = RouteServiceProvider::DASHBOARD;

Make a final change in app\Http\Middleware\RedirectIfAuthenticated.php

 public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {
            return redirect(RouteServiceProvider::HOME);
        }

        return $next($request);
    }

To

public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {
            return redirect(RouteServiceProvider::PARTNERS);
        }

        return $next($request);
    }

Change your rout to

Route::get('/', function () {
    return redirect('/dashboard');    
})->middleware('auth');

Not open the CMD and inside the project folder and run the command php artisan optmize:clear and check now by the login.