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
How to Access a Deep Object Property Named as a Variable (Dot Notation) in PHP
PHP - Get Key Name of Array Value
Scaling a Chat App - Short Polling VS. Long Polling (Ajax, PHP)
Configure Multiple Databases in Zf2
PHP - Indirect Modification of Overloaded Property
Ajax Post Request in Laravel 5 Return Error 500 (Internal Server Error)
How to Pass an Array in Get in PHP
Returning Multiple Rows with MySQLi and Arrays
Mysql_Num_Rows() Expects Parameter 1 to Be Resource, Boolean Given In
Having Problems While Try to Install Oauth with Pecl in Mamp on MAC Os Lion
Encrypt Files Using Pgp in PHP
How to Check If $_Get Is Empty
Php: How to Disable Dangerous Functions
Access a File Which Is Located Before/Outside the Server Root Directory