I tried logging in a user (email and password) to the user’s dashboard page but got the error message:
Target class [AppHttpControllersappdashboarddashboardController] does not exist, after clicking the login button.
My expectations:
After a user has logged in and is authenticated the user is to be redirected to the dashboard page.
But instead I get this error:
Target class [AppHttpControllersappdashboarddashboardController] does not exist.
Look at the full description of what I’ve done:
I’ve written this code:
<x-layout.guest-layout>
<form action="{{route('login.func')}}" method="post">
@csrf
<main class="flex flex-col justify-center items-center gap-3 py-4">
<div>
<h4>Login</h4>
</div>
@if (Session('success'))
<p class="text-green-500 font-bold p-2 flex flex-row justify-center item-center">
{{Session('success')}}
</p>
@endif
@if (Session('error'))
<p class="text-red-500 font-bold p-2 flex flex-row justify-center item-center">
{{Session('error')}}
</p>
@endif
@if (Session('exist'))
<p class="text-gray40 font-bold p-2 flex flex-row justify-center item-center">
{{Session('exist')}}
</p>
@endif
<div class="flex flex-col justify-start w-1/2">
<label for="email">Email</label>
<input class="rounded-md p-1 border focus:outline-green-500" type="email" name="email" id="email">
{{-- error directives --}}
@error('email')
<div class="font-semibold text-red-500 text-sm">
{{$message}}
</div>
@enderror
</div>
<div class="flex flex-col justify-start w-1/2">
<label for="password">Password</label>
<input class="rounded-md p-1 border focus:outline-green-500" type="password" name="password" id="password">
@error('password')
<div class="font-semibold text-red-500 text-sm">
{{$message}}
</div>
@enderror
</div>
<div class="flex flex-col gap-2 w-1/2">
<button class="text-white bg-green-500 font-bold rounded-md p-1">Login</button>
<a href="/register" class="text-green-400 font-semibold">Create an Account</a>
</div>
</main>
</form>
</x-layout.guest-layout>
within this file below:
Login page file location
that allows users to login, sending their credentials to be authenticated, into the file below:
Location of authentication controller file
which gets handled by the loginFunc method below:
<?php
namespace AppHttpControllersappauth;
use AppHttpControllersController;
use AppModelsUserModel;
use AppHttpControllersappdashboarddashController;
use IlluminateHttpRequest;
use IlluminateValidationValidationException;
use IlluminateSupportFacadesHash;
use IlluminateSupportFacadesSession;
class authController extends Controller
{
public function loginPage(){
if (Session::get('userID')) {
return redirect('/dashboard');
}
return view("app.pages.auth.loginPage");
}
public function registerPage(){
if (Session::get('userID')) {
return redirect('/dashboard');
}
return view("app.pages.auth.registerPage");
}
// public function dashboardPage(){
// if (!Session::get('userID')) {
// return Back()->with('', 'Access Denied');
// }
// return view('app.pages.dashboard.protectedDashPage');
// }
public function loginFunc(Request $request)
{
try {
$request->validate([
'email' => ['required', 'email'],
'password' => ['required', 'string', 'min:8', 'max:15']
]);
$existingUser = UserModel::where('email', $request->email)->first();
if (!$existingUser /**$exitingUser->password !== $request->password*/) {
return redirect('/')->with('exist', 'User does not exist');
}elseif(!Hash::check($request->password, $existingUser->password)){
return redirect('/')->with('error', 'Invalid email or password');
}
$request->session()->start();
$request->session()->put('userID', $existingUser->id);
return redirect('/dashboard');
} catch (ValidationException $e) {
return back()->withErrors($e->validator->errors());
}//;
}
using this route:
// Route to controller that processes form's input
Route::post('/login', [authController::class, 'loginFunc'])->name('login.func');
The loginFun authenticates the user, and if the user is authenticated, redirects the user to the dashboard page file:
location of dasboard bage file
Using the the last route in the code below:
<?php
use AppHttpControllersappauthauthController;
use AppHttpControllersappdashboarddashController;
use IlluminateSupportFacadesRoute;
// Route to pages
Route::get('/', [authController::class, 'loginPage'])->name('index.page');
Route::get('/register', [authController::class, 'registerPage'])->name('register.page');
Route::get('/about', [dashController::class, 'aboutPage'])->name('about.page');
Route::middleware('authM')->group(function () {
Route::get('/dashboard',[dashController::class, 'dashboardPage'])->name('dashboard.page');
});
route to dashboard page file
This route points to two files. First the authM middleware file below:
Location of authM middleware file
Having the code below:
<?php
namespace AppHttpMiddleware;
use Closure;
use IlluminateHttpRequest;
use SymfonyComponentHttpFoundationResponse;
use IlluminateSupportFacadesSession;
class authM
{
/**
* Handle an incoming request.
*
* @param Closure(IlluminateHttpRequest): (SymfonyComponentHttpFoundationResponse) $next
*/
public function handle(Request $request, Closure $next): Response
{
if (!Session::get("userID")) {
return redirect('/')->with('error', 'Please Login');
}
return $next($request);
}
}
That prevents any unauthorise ascess to the dashboard page, and the dashController contoller file below:
Loction of the dashboard controller file
That contains the dashboardPage method below:
<?php
namespace AppHttpControllersappdashboard;
use AppHttpControllersController;
use IlluminateHttpRequest;
use IlluminateSupportFacadesSession;
class dashController extends Controller
{
public function aboutPage()
{
return view('app.pages.dashboard.about');
}
public function dashboardPage(){
return view('app.pages.dashboard.protectedDashPage');
}
}
Which renders the dashboard page (protectedDashPage.blade.php).
But I just keep getting the error:
Target class [AppHttpControllersappdashboarddashboardController] does not exist.
error message
Worldlight Idakpo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.