我试图制作登录页面。通过使用middleware
,当我单击“登录”按钮时,我应该能够重定向到主页,但是当我单击“登录”按钮时,它的返回重定向将回到我的登录页面。这是我的代码
Authenticate.php(中间件)
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
return route('login');
}
}
web.php(路由)
Route::get('/', 'AuthController@index')->name('login');
Route::post('/login', 'AuthController@login')->middleware('auth')->name('welcome');
AuthController.php
public function index(Request $request) {
return view('auth.login');
}
public function login(Request $request)
{
$username = $request->username;
$pass = $request->password;
$password = base64_encode(sha1($pass, true));
$options = [
'headers' => [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
],
'query' => [
'username' => $username,
'password' => $password,
]
];
$response = $this->client->post('userLogin', $options)->getBody();
$content = json_decode($response->getContents());
if($content == null)
{
return redirect()->back()->with('error', 'Username not exist');
}
return view('welcome.dashboard'); //expected redirect to dashboard after login
}
登录数据来自后端api。我只是通过使用guzzle提取数据。很抱歉,如果我的解释不清楚,但我希望它能为您提供帮助,对不起我的英语不好]
如果尝试在dd(1);
内部使用,可能是重定向回了但没有打印消息'error'