Class 'App\Http\Controllers\Admin\Auth' Not Found in Laravel 5

Class 'App\Http\Controllers\admin\Auth' not found in Laravel 5

Because your controller is namespaced unless you specifically import the Auth namespace, PHP will assume it's under the namespace of the class, giving this error.

To fix this, add use Auth; at the top of AdminHomeController file along with your other use statements or alternatively prefix all instances of Auth with backslash like this: \Auth to let PHP know to load it from the global namespace.

Laravel error: Class 'App\Http\Controllers\Auth\Auth' not found for multi authentication

When referencing class names in PHP, they all act as relative, meaning class names will be appended to whatever the current namespace is.

If you are not in a namespace, and you look for the class Auth, PHP will attempt to find the fully-qualified class name (namespace + class) of Auth. If you are in the namespace App and you look for the class Auth, PHP will attempt to find App\Auth, or the Auth class defined inside the App namespace.

Because your controller is inside the namespace App\Http\Controllers\Auth, it is looking inside this same namespace for the class Auth.

In order to look for a different namespace, via an absolute look up, you can either prepend the class with a \ backslash (start from the root), or add a use import statement.

Following the example in Laravel's docs, adding a use statement for the appropriate facade should resolve your error:

use Illuminate\Support\Facades\Auth;

This can be placed along with the other use import statements in your class. Happy coding!

Class 'App\Http\Controllers\Auth\User' not found

You didnt import Auth namespace the right way.

The proper namespace is Illuminate\Support\Facades\Auth;

Add use Illuminate\Support\Facades\Auth; at the top of your class.

laravel9 middleware not loaded when frontend and admin are seperated

To keep the frontend and admin separate, basically need:

  • separate routes file (say web.php, api.php & admin.php)
  • separate redirectTo routes in RedirectIfAuthenticated
  • separate redirectTo routes in Authenticate

To achieve that keep all default middleware & providers under app folder -

|-app
|-Http
|-Middleware
-Authenticate.php
-EncryptCookies.php
-PreventRequestsDuringMaintenance.php
-RedirectIfAuthenticated.php
-TrimStrings.php
-TrustHosts.php
-TrustProxies.php
-VerifyCsrfToken.php
|-Providers
-AppServiceProvider.php
-AuthServiceProvider.php
-BroadcastServiceProvider.php
-EventServiceProvider.php
-RouteServiceProvider.php
|-Frontend
|-Http
|-Controllers
|-FrontendControllers
|-Middleware
|-FrontendSpecificMiddleware (if any)
|-Requests
|-FrontendFormRequests (if any)
|-Languages
|-Other
|-Admin
|-Http
|-Controllers
|-AdminControllers
|-Middleware
|-AdminSpecificMiddleware (if any)
|-Requests
|-AdminFormRequests (if any)
|-Languages
|-Other
|-routes
|-web.php

To load routes, in App\Providers\RouteServiceProvider


$this->routes(function () {
Route::middleware('api')
->prefix('api')
->group(base_path('routes/api.php'));

Route::middleware('web')
->group(base_path('routes/web.php'));

Route::middleware('web')
->prefix('admin')
->as('admin.')
->group(app_path('Admin/routes/web.php'));
});

To set the redirect route for successful authentication

<?php

namespace App\Http\Middleware;

use App\Providers\RouteServiceProvider;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class RedirectIfAuthenticated
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @param string|null ...$guards
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next, ...$guards)
{
$guards = empty($guards) ? [null] : $guards;

foreach ($guards as $guard) {
if (Auth::guard($guard)->check()) {
$prefix = $request->route()->getPrefix();
return $prefix == '/admin' ? redirect(route('admin.home') : redirect(RouteServiceProvider::HOME);
}
}

return $next($request);
}
}

To set redirect route for unsuccessful authentication

<?php

namespace App\Http\Middleware;

use Illuminate\Auth\Middleware\Authenticate as Middleware;

class Authenticate extends Middleware
{
/**
* Get the path the user should be redirected to when they are not authenticated.
*
* @param \Illuminate\Http\Request $request
* @return string|null
*/
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
$prefix = $request->route()->getPrefix();
return $prefix == '/admin' ? route('admin.login') : route('login);
}
}
}

You can also have App\Frontend\FrontendServiceProvider and App\Admin\AdminServiceProvider if you want and register them under providers array in config/app.php

To summarise, take cue from Domain Driven Design (DDD) approach. You can read about DDD at:

Stitcher - Laravel Beyond Crud - Domain Oriented Laravel

Conciliating Laravel and DDD

Class 'Auth' not found in laravel 6

The Solution Was adding The use Auth in my Controller! That's All



Related Topics



Leave a reply



Submit