Home>

I am currently making a multi-login function with laravel.
The following error message occurred while implementing the login and registration functions for teachers.

Target class [Professor \ LoginController] does not exist.
route/web.php
<? php
use Illuminate \ Support \ Facades \ Route;
use App \ Http \ Controllers \ Professor \ LoginController;
Auth :: routes ();
/ *
| ------------------------------------------------- -------------------------
| user Before login
| ------------------------------------------------- -------------------------
|
* /
Route :: get ('/', function () {
    return view ('welcome');
});
/ *
| ------------------------------------------------- -------------------------
| user After logging in
| ------------------------------------------------- -------------------------
|
* /
Route :: group (['middleware'=>'auth: user'],

 function () {
    Route :: get ('/ home', [App \ Http \ Controllers \ HomeController :: class,'index'])->name ('home');
});
/ *
| ------------------------------------------------- -------------------------
| professor before login
| ------------------------------------------------- -------------------------
|
* /
Auth :: routes ();
Route :: group (['prefix' =>'professor'],

 function () {
    Route :: get ('/', function () {return redirect ('home');});
    Route :: get ('/ login','Professor \ LoginController @ showLoginForm')->name ('professor.login');
    Route :: get ('/ login', [LoginController :: class,'showLoginForm'])->name ('Professor.login');
    Route :: get ('/ register','Professor \ LoginController @ showRegisterForm')->name ('professor.register');
    Route :: post ('/ login','Professor \ LoginController @ login');
});

/ *
| ------------------------------------------------- -------------------------
| professor After logging in
| ------------------------------------------------- -------------------------
|
* /
Route :: group (['prefix' =>'professor','middleware' =>'auth: professor'],

 function () {
    Route :: post ('logout', [App \ Http \ Controllers \ LoginController :: class,'logout'])->name ('professor.logout');
    Route :: get ('/ home', [App \ Http \ Controllers \ HomeController :: class,'index'])->name ('home');
});
App/Http/Controllers/Professor/LoginController.php
<? php
namespace App \ Http \ Controllers \ Professor;
use App \ Http \ Controllers \ Controller;
use App \ Providers \ RouteServiceProvider;
use Illuminate \ Http \ Request;
use Illuminate \ Support \ Facades \ Auth;
use Illuminate \ Foundation \ Auth \ AuthenticatesUsers;

class LoginController extends Controller
{
    use AuthenticatesUsers;
   /**
     * Where to redirect users after login.
     *
     * @var string
     * /
    protected $redirectTo = RouteServiceProvider :: HOME;
   /**
     * Create a new controller instance.
     *
     * @return void
     * /
    public function __construct ()
    {
        $this->middleware ('guest: professor')->except ('logout');
    }

    public function showLoginForm ()
    {
        return view ('professor.login');
    }
    protected function guard ()
    {
        return Auth :: guard ('professor');}
    public function logout (Request $request)
    {
        // Auth :: guard ('professor')->logout ();
        $request->session ()->flush ();
        $request->session ()->regenerate ();
        return redirect ('/ professor/login');
    }
}
App/Http/Controllers/Professor/HomeController.php
<? php
namespace App \ Http \ Controllers \ Professor;
use Illuminate \ Http \ Request;
use App \ Http \ Controllers \ Controller;
class HomeController extends Controller
{
   /**
     * Create a new controller instance.
     *
     * @return void
     * /
    public function __construct ()
    {
        $this->middleware ('auth: professor');
    }
   /**
     * Show the application dashboard.
     *
     * @return \ Illuminate \ Contracts \ Support \ Renderable
     * /
    public function index ()
    {
        return view ('professor: home');
    }
}
config/auth.php
<? php
return [
   /*
    | ------------------------------------------------- -------------------------
    | Authentication Defaults
    | ------------------------------------------------- -------------------------
    |
    | This option controls the default authentication "guard" and password
    | reset options for your application. You may change these defaults
    | as required, but they're a perfect start for most applications.
    |
    * /
    'defaults' =>[
        'guard' =>'user',
        'passwords' =>'users',
    ],,


   /*
    | ------------------------------------------------- -------------------------
    | Authentication Guards
    | ------------------------------------------------- -------------------------
    |
    | Next, you may define every authentication guard for your application.
    | Of course, a great default configuration has been defined for you
    | here which uses session storage and the Eloquent user provider.
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | Supported: "session", "token"
    |
    * /
    'guards' =>[
        'web' =>[
            'driver' =>' session',
            'provider' =>'users',
        ],,


        'api' =>[
            'driver' =>'token',
            'provider' =>'users',
            'hash' =>false,
        ],,


        'user' =>[
            'driver' =>' session',
            'provider' =>'users',
        ],,


        'professor' =>[// Add
            'driver' =>' session', // added
            'provider' =>'professors', // added
        ],,],,


   /*
    | ------------------------------------------------- -------------------------
    | User Providers
    | ------------------------------------------------- -------------------------
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | If i have multiple user tables or models you may configure multiple
    | sources which represent each model/table. These sources may then
    be assigned to any extra authentication guards you have defined.
    |
    | Supported: "database", "eloquent"
    |
    * /
    'providers' =>[
        'users' =>[
            'driver' =>'eloquent',
            'model' =>App \ Models \ User :: class,
        ],,


        'professors' =>[// Add
            'driver' =>'eloquent', // added
            'model' =>App \ Models \ Professor :: class, // Added
        ],,


        //'users' =>[
        //'driver' =>'database',
        //'table' =>'users',
        //],


    ],,


   /*
    | ------------------------------------------------- -------------------------
    | Resetting Passwords
    | ------------------------------------------------- -------------------------
    |
    | You may specify multiple password reset configurations if you have more
    | than one user table or model in the application and I want to have
    | separate password reset settings based on the specific user types.
    |
    | The expire time is the number of minutes that the reset token should be
    | considered valid. This security feature keeps tokens short-lived so
    | they have less time to be guessed. You may change this as needed.
    |
    * /
    'passwords' =>[
        'users' =>[
            'provider' =>'users',
            'table' =>' password_resets',
            'expire' =>60,
            'throttle' =>60,
        ],,


        'professors' =>[// Add
            'provider' =>'professors', // added
            'table' =>'password_resets', // added
            'expire' =>60, // Added
        ],,


    ],,


   /*
    | ------------------------------------------------- -------------------------
    | Password Confirmation Timeout
    | ------------------------------------------------- -------------------------
    |
    | Here you may define the amount of seconds before a password confirmation
    | times out and the user is prompted to re-enter their password via the
    | confirmation screen. By default, the timeout lasts for three hours.
    |
    * /
    'password_timeout' =>10800,
];;
What I tried

I checked the controller namespace, routing spelling, etc. but it didn't work. I tried to rewrite it considering that the description method of routing changed from laravel 8.x, but it did not change.
I would be grateful if you could tell me where the cause is. Thank you.

Supplementary information (FW/tool version, etc.)

laravel 8.4.0
PHP 7.4.10

  • Answer # 1

    Route :: group (['prefix'=>' professor'], function () {
        Route :: get ('/', function () {return redirect ('home');});
        Route :: get ('login', [App \ Http \ Controllers \ Professor \ LoginController :: class,'showLoginForm'])->name ('professor.login');
        Route :: post ('login', [App \ Http \ Controllers \ Professor \ LoginController :: class,'login']);
        Route :: get ('register', [App \ Http \ Controllers \ Professor \ RegisterController :: class,'showRegisterForm'])->name ('professor.register');
    });


    I was able to solve it here