PROBLEM
我正忙于我的第一个 Laravel 应用程序,尽管我看到了这种编写方式的好处,但我很难理解其中的一些行为。
当我尝试登录时,我被重定向到登录页面。看起来用户身份验证正确,但无论如何它都会重定向到登录页面。
我拥有的
我的用户表如下所示:
,---------,---------------,---------------,----------------,---------------,-----------,
| user_id | user_username | user_password | user_firtsname | user_lastname | user_type |
|---------|---------------|---------------|----------------|---------------|-----------|
| 1 | [email protected] /cdn-cgi/l/email-protection | encrypted | Foo | Bar | farmer |
'---------'---------------'---------------'----------------'---------------'-----------'
这是我的路线文件:
<?php
Route::get('/login', 'UsersController@login');
Auth::routes();
Route::get('/dashboard', 'HomeController@dashboard')->name('dashboard');
Route::get('/users/grid', 'UsersController@grid');
Route::resource('/users', 'UsersController');
登录控制器.php
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* @var string
*/
protected $redirectTo = '/dashboard';
public function __construct()
{
$this->middleware('guest')->except('logout');
}
/**
* @return string
*/
public function username()
{
return 'user_username';
}
/**
* @param Request $request
* @param $user
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function authenticated(Request $request, $user)
{
return $user->user_type === 'farmer' ? redirect('/dashboard') : redirect('/admin');
}
}
User.php
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* @var array
*/
protected $fillable = [
'user_username', 'user_firstname', 'user_lastname', 'user_password',
];
/**
* @var array
*/
protected $hidden = [
'user_password', 'remember_token',
];
/**
* @var bool
*/
public $timestamps = false;
/**
* @return mixed|string
*/
public function getAuthPassword()
{
return $this->user_password;
}
/**
* @return string
*/
public function getKey()
{
return $this->user_id;
}
}
我做了什么
我读过各种问题 https://stackoverflow.com/questions/45191677/laravel-5-4-redirect-after-authentication on https://stackoverflow.com/questions/35499929/laravel-5-redirecting-to-login-page-after-authentication-instead-of-dashboard 堆栈溢出 https://stackoverflow.com/questions/42177044/laravel-5-4-redirection-to-custom-url-after-login但由于某种原因我无法登录工作。
我使用创建了身份验证php artisan make:auth
。我也尝试阅读文档,但仍然没有成功。
QUESTION
登录后如何使其重定向到仪表板?我缺少什么?