我设置了一个自定义的警卫和登录控制器
登录很好,但不知怎么的,它没有存储经过身份验证的用户
我读过这个"Auth::user()在Laravel 5.2中返回null"
并且这个"Auth::user()返回null"
但我认为我的问题与中间件无关
代码如下:
控制器:
<?php
namespace App\Http\Controllers\GuruAuth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class LoginController extends Controller
{
use AuthenticatesUsers;
protected $redirectTo = RouteServiceProvider::GURU_HOME;
public function showLoginForm(Request $request)
{
return view('auth_guru.login');
}
protected function guard()
{
return Auth::guard('guru');
}
public function username()
{
return 'username';
}
public function __construct()
{
$this->middleware('guest:guru')->except('logout');
}
型号:
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Akun;
class Guru extends Akun
{
protected $table = 'guru';
protected $guard = 'guru';
protected $fillable = [
'nip', 'nama', 'id_mapel',
'username', 'password',
];
public function mapel()
{
return $this->belongsTo('App\Mapel', 'id_mapel');
}
}
config/auth.php:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
'admin' => [
'driver' => 'session',
'provider' => 'admin',
],
'guru' => [
'driver' => 'session',
'provider' => 'guru',
],
'ortu' => [
'driver' => 'session',
'provider' => 'ortu',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admin' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
'guru' => [
'driver' => 'eloquent',
'model' => App\Guru::class,
],
'ortu' => [
'driver' => 'eloquent',
'model' => App\Ortu::class,
],
],
路线表:
+--------+----------+------------------------+----------------------+------------------------------------------------------------------------+-----------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+------------------------+----------------------+------------------------------------------------------------------------+-----------------+
| | GET|HEAD | admin/dashboard | admin.dashboard | Closure | web |
| | GET|HEAD | admin/login | admin.login | App\Http\Controllers\AdminAuth\LoginController@showLoginForm | web,guest:admin |
| | POST | admin/login | admin.login.submit | App\Http\Controllers\AdminAuth\LoginController@login | web,guest:admin |
| | GET|HEAD | api/user | | Closure | api,auth:api |
| | GET|HEAD | guru/dashboard | guru.dashboard | Closure | web,auth |
| | GET|HEAD | guru/login | guru.login | App\Http\Controllers\GuruAuth\LoginController@showLoginForm | web,guest:guru |
| | POST | guru/login | guru.login.submit | App\Http\Controllers\GuruAuth\LoginController@login | web,guest:guru |
| | GET|HEAD | home | home | App\Http\Controllers\HomeController@index | web,auth |
| | POST | login | | App\Http\Controllers\Auth\LoginController@login | web,guest |
| | GET|HEAD | login | login | App\Http\Controllers\Auth\LoginController@showLoginForm | web,guest |
| | POST | logout | logout | App\Http\Controllers\Auth\LoginController@logout | web |
| | GET|HEAD | murid | murid.index | App\Http\Controllers\MuridController@index | web |
| | PUT | murid/data-diri | murid.update | App\Http\Controllers\MuridController@update | web |
| | GET|HEAD | murid/data-diri | murid.edit | App\Http\Controllers\MuridController@edit | web |
| | GET|HEAD | murid/jadwal | jadwal.index | App\Http\Controllers\JadwalController@index | web |
| | POST | murid/jadwal | jadwal.store | App\Http\Controllers\JadwalController@store | web |
| | GET|HEAD | ortu/dashboard | ortu.dashboard | Closure | web |
| | GET|HEAD | ortu/login | ortu.login | App\Http\Controllers\OrtuAuth\LoginController@showLoginForm | web,guest:ortu |
| | POST | ortu/login | ortu.login.submit | App\Http\Controllers\OrtuAuth\LoginController@login | web,guest:ortu |
| | GET|HEAD | ortu/register | ortu.register | App\Http\Controllers\OrtuAuth\RegisterController@showRegisterForm | web,guest |
| | POST | ortu/register | ortu.register.submit | App\Http\Controllers\OrtuAuth\RegisterController@register | web,guest |
| | GET|HEAD | password/confirm | password.confirm | App\Http\Controllers\Auth\ConfirmPasswordController@showConfirmForm | web,auth |
| | POST | password/confirm | | App\Http\Controllers\Auth\ConfirmPasswordController@confirm | web,auth |
| | POST | password/email | password.email | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail | web |
| | GET|HEAD | password/reset | password.request | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm | web |
| | POST | password/reset | password.update | App\Http\Controllers\Auth\ResetPasswordController@reset | web |
| | GET|HEAD | password/reset/{token} | password.reset | App\Http\Controllers\Auth\ResetPasswordController@showResetForm | web |
| | GET|HEAD | register | register | App\Http\Controllers\Auth\RegisterController@showRegistrationForm | web,guest |
| | POST | register | | App\Http\Controllers\Auth\RegisterController@register | web,guest |
+--------+----------+------------------------+----------------------+------------------------------------------------------------------------+-----------------+
我还尝试将此添加到控制器:
public function authenticated(Request $request, $user)
{
$user = $this->guard()->user();
Auth::setUser($user);
}
当我试图从视图访问Auth::user()时,它返回null。
该视图直接从route/web.php调用:
Route::group(['prefix' => 'guru'], function() {
Route::get('/login', 'GuruAuth\LoginController@showLoginForm')->name('guru.login');
Route::post('/login', 'GuruAuth\LoginController@login')->name('guru.login.submit');
Route::get('/dashboard', function() {
return view('guru.index');
})->name('guru.dashboard');
});
因此,我不知道为什么会发生这种情况,在最终放弃之后,我想出了一个(不太干净的)解决方案。
现在,我不再使用多个守卫,而是创建一个从用户表到guru/admin表的关系。
这就是我现在访问它的方式Auth::user()-
我知道这不是一个好的解决方案,但这是我能找到的最快的解决方法
我使用Laravel 5.2,中间件有问题。routes.php里有密码 Kernel.php: AdminPanel.php 所以,
我搜索并找到了如下各种结果:auth()- 但是,我的仍然不起作用。 在控制器中工作,但在模型中不工作。它返回。 代码是: 我尝试,它也返回。 任何想法?
我在Laravel中创建了多重身份验证。当我使用用户登录时,在post login方法中调试时,在Auth::guard('user')之后- 如何解决?我是拉威尔的初学者。 谢谢!!! /路由/auth/user。php }); /路由/网络。php }); /app/Controllers/User/Auth/LoginController-@post登录 /应用程序/控制器/用户/家庭控制器
我有一个函数 当我dd时,即使我在控制器上声明了auth facade,它也会返回null 这是我的路线,它存储在
问题: 在我的索引页面上,经过身份验证的用户和来宾应该能够查看和使用该页面,除非他们想保存某些内容。我试图登录以便保存数据,但被重定向到一个经过身份验证的页面,这是一个配置文件页面。我尝试在配置文件页上转储,它返回用户登录的数据,尝试在索引页上再次转储,然后返回null 我正在使用最新版本的Laravel。 我在谷歌上搜索了一些与我遇到的问题相同的问题,最接近的问题是Auth::user()返回n
当用户登录时,被设置,但是返回null。 这意味着我必须在需要访问用户的属性或方法的任何地方这样做: 有什么更好的解决方案?我是否可以在BaseController中设置,然后通过