Laravel 说 Auth guard [] 没有定义
Posted
技术标签:
【中文标题】Laravel 说 Auth guard [] 没有定义【英文标题】:Laravel say that Auth guard [] is not defined 【发布时间】:2016-03-11 00:44:44 【问题描述】:我是初学者,我开始使用 laravel 学习和编码... 为了启用用户登录和注册,我写了这个(正如我在一个教程中看到的):
在 routes.php
Route::controllers([
'auth'=>'Auth\AuthController',
'password'=>'Auth\PasswordController', ]);
现在当我输入:http://localhost:8888/auth/login 我得到错误:
AuthManager.php 第 71 行中的 InvalidArgumentException: 身份验证守卫 [] 是 未定义。
view文件夹中也没有auth目录和login.blade.php等文件。
【问题讨论】:
我看不出你的路线有什么问题,但是 Laravel 5.1 不再包含授权文件。但是,您可以从以下地址获取它们:github.com/bestmomo/scafold @craig_h 文件身份验证控制器文件是 included 与 Laravel,只有欢迎控制器被删除。 @MonkeyBusiness 您是否对AuthManager
进行了任何更改?因为该文件的on line 71 有一个不会引发异常的 PHPDoc 块。
@Bogdan 啊,是的,我的意思是不再包含 auth view
文件,而不是控制器,这是对“同样在视图文件夹中也没有 auth 目录”的直接响应。重要的澄清。
@MonkeyBusiness 以后请确保您正确标记您的问题。因为你的问题有laravel-5.1 标签。
【参考方案1】:
如果您已从 5.1.x 升级到 5.2,请确保您的 config/auth.php 已更新。
https://github.com/laravel/laravel/blob/v5.2.0/config/auth.php
【讨论】:
【参考方案2】:如果您编辑了您的 config/auth.php
,例如要添加另一个守卫并缓存您的配置,您的守卫可能不会重新加载。如果您遇到此问题,清除配置即可解决。
$php artisan config:clear
或 $php artisan config:cache
我使用的是 laravel 5.5
【讨论】:
我在这个错误上花了几个小时,非常感谢【参考方案3】:这可能是您的 config/auth.php 文件中的问题,其中“默认”数组在 Laravel 5.2 上设置了一个不存在的保护。
【讨论】:
这应该是一条评论【参考方案4】:程序目录App/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' => 'web',
'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',
],
// define your Auth here..
'your_auth_name' => [
'driver' => 'session',
'provider' => 'your table name',
],
],
/*
|--------------------------------------------------------------------------
| 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 you 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\User::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
// add provider to your auth
'table name' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
// **'model' => App\User::class,**
// here App\User is model so you have to generate own model using **php artisan make:model Model_name**
],
/*
|--------------------------------------------------------------------------
| Resetting Passwords
|--------------------------------------------------------------------------
|
| Here you may set the options for resetting passwords including the view
| that is your password reset e-mail. You may also set the name of the
| table that maintains all of the reset tokens for your application.
|
| You may specify multiple password reset configurations if you have more
| than one user table or model in the application and you 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',
'email' => 'auth.emails.password',
'table' => 'password_resets',
'expire' => 60,
],
], ];
【讨论】:
请添加一些描述 我已经将代码中的描述定义为带有代码示例的注释。如果您有任何困惑,请询问。【参考方案5】:此问题的主要原因是您的系统无法检测到新创建的保护。 跑吧
php artisan config:clear
php artisan config:cache
如果您无法在共享主机中运行 artisan 命令或项目,请将以下代码写入您的 web.php 文件
Route::get('/clear', function()
Artisan::call('cache:clear');
Artisan::call('config:clear');
Artisan::call('config:cache');
Artisan::call('view:clear');
Artisan::call('route:clear');
return "Cleared!";
);
现在,在你的基本网址后面写上“clear”并回车。希望你的问题能解决。
【讨论】:
不错的方法:)【参考方案6】:清除您的配置和缓存
php 工匠配置:清除
php 工匠配置:缓存
【讨论】:
您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。以上是关于Laravel 说 Auth guard [] 没有定义的主要内容,如果未能解决你的问题,请参考以下文章
Laravel auth()->guard() 使用 vue js 返回 null
Laravel AppServiceProvider Auth::guard('admin')->check() 不工作
call_user_func_array() 期望参数 1 是一个有效的回调,类 'Illuminate\Auth\Guard' 没有方法 'attemp' - Laravel 中的 Auth