Target class [App\Http\Controllers\LoginController] does not exist

Viewed 30525

Laravel 6.x. I'm creating custom multi-authentication Panels(Staff,Student,Admin) from Single Login Page.Error already mentioned in the title. also without using php artisan ui bootstrap --auth. Web.php file.

Route::get('/index/sign-in', function () {
    return view('log-in');
});
Route::get('/index/admin', function () {
    return view('admin-dashboard');
});
Route::get('/index/student', function () {
    return view('student-dashboard');
});
Route::get('/index/staff', function () {
    return view('faculty-dashboard');
});
Route::middleware('auth')->group(function () {
    Route::post('/index/dashboard/','LoginController@postlogin')->name('postlogin');
    Route::post('/index/logout','LoginController@postlogout')->name('postlogout');
});

LoginController.php file

    public function postlogout()
    {
        auth()->logout();
        //session()->flash('message', 'Some goodbye message');
        return redirect('/index/sign-in/');
    }
    public function postlogin()
    {
        $role=(Auth::user())->user_role;
        if ($role=='admin'){
            return 'index/admin';
        }
        elseif ($role=='staff'){
            return 'index/staff';
        }
        elseif ($role=='student'){
            return 'index/student';
        }else
        return 'index/sign-in';
    }
}
6 Answers

If you didn't move the controller from his default location, is inside the Auth folder, in the controller folder (app/Http/Controllers/Auth) So as your error Target class [App\Http\Controllers\LoginController] does not exist it's searching for the controller in the Controllers folders but not in the Auth subfolder, so The route is wrong.

Route::post('/index/logout','LoginController@postlogout')->name('postlogout');

should be

Route::post('/index/logout','Auth\LoginController@postlogout')->name('postlogout');

Best Regards.

I'm not sure if this is the default in Laravel 6, but the LoginController is likely under the Auth folder/namespace`:

app
- Http
-- Controllers
--- Auth
---- LoginController.php
...

In this case, you need to reference the namespace in your routes:

Route::middleware('auth')->group(function () {
    Route::post('/index/dashboard/', 'Auth\LoginController@postlogin')->name('postlogin');
    Route::post('/index/logout', 'Auth\LoginController@postlogout')->name('postlogout');
});

If you did not create the controller with the artisan command, delete it and create it with the php artisan create:controller LoginController command. This should get the problem resolved.

Can you check the namespace of the controller namespace App\Http\Controllers\Auth;

class LoginController extends Controller

All the above solution didn't work for me, what did was using

php artisan route:clear

In your web.php file check all namespace passed, if their directory root is passed correctly or not.

in the upper part of web.php file you need to make sure of each controller root. Such as you are using LoginController so you must pass use App\Http\Controllers\LoginController; or use App\Http\Controllers\Controller\LoginController; according to your Http\Controller project structure

Related