Redirection problem after login

  Fri 28 / 04 / 2023

  Posted by: Vanita saini

Category : Laravel

Tags : laravel , php , php laravel

Post Thumbnail
Description

web.php

<?php

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Auth\PostController;
use App\Http\Controllers\HspcbWebsiteController;
use App\Http\Controllers\Auth\DashboardController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Auth::routes([
    'register'=>false
]);

//Route::view('/theme','layouts.auth');
Route::view('/adm','auth.dashboard')->middleware('auth');

Route::controller(HspcbWebsiteController::class)->group(function(){
    Route::get('/', 'home')->name('home');
    Route::get('/post/{post}','show')->name('hswebsite.posts.show');
});

Route::prefix('auth')->middleware(['auth'])->group(function(){
    //Route::view('/','auth.dashboard');
    Route::get('/dashboard', [DashboardController::class, 'dashboard'])->name('dashboard')->middleware('auth');
    Route::resource('/posts', PostController::class);
});




Login Controller

<?php

namespace App\Http\Controllers\Auth;

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

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    public const REDIRECTTO = 'auth.dashboard';
    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = LoginController::REDIRECTTO;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
}

dasdsdsd


View Code
                        Sir, I m getting issues after login. when i click on login page after that its goes to dashboard (http://laravel.hspcb/auth.dashboard).  but it not showing that page. Could u please tell me my web.php file code is ok or not?

if i access auth section with some other URL like which I have mentioned in web.php
http://laravel.hspcb/adm.. then it works fine.. but if I direct open page like http://laravel.hspcb/login...
then its goes to http://laravel.hspcb/auth.dashboard this URL and showing 404  not found.
                
  Answered by CDL

@Vanita you are working with the route and not passing the route helper, you passed the route name without the route helper.

You passed auth.dashboard instead of route('auth.dashboard'), so it will not generate the qualified url.

Remember in the controller you can not use this route helper, so how to add the path in the RedirectTo property?

~~~

public const REDIRECTTO =  'auth/dashboard';

~~~

Simply add it as a hardcoded URL as Laravel does itself by default.

Solved
  Comment   Share
0 Likes   0 Comments