Я установил настраиваемую охрану и контроллер входа в систему
Вход в систему в порядке, но каким-то образом он не сохраняет аутентифицированного пользователя
Я прочитал это "Auth :: user () возвращает ноль в Laravel 5.2 "
, и это " Auth :: user () возвращает ноль "
Но я думаю, что моя проблема не имеет ничего общего с промежуточным ПО
Вот код:
Контроллер:
<?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 ( ) из представления возвращается ноль.
Вид вызывается напрямую из маршрута / сети. 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');
});
РЕДАКТИРОВАТЬ: Кристоф Хьюберт, вот скриншот после того, как я переопределил функцию: На самом деле это то же самое, что и раньше