【发布时间】:2019-02-25 08:04:02
【问题描述】:
我的 web.php 文件中有以下路由:
Route::get('/', 'PostsController@index')->name('home');
Route::get('/login', 'SessionsController@show');
SessionController 类有一个公共构造函数,它使用 Laravel 的中间件来检查用户是否是访客:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class SessionsController extends Controller
{
public function __construct() {
$this->middleware('guest')->except(['destroy']);
public function create() {
// Validade form data
$this->validate(request(), [
'email' => 'required',
'password' => 'required'
]);
// Attempt to authenticate the user
if(! auth()->attempt(request(['email', 'password']))) {
return back()->withErrors([
'message' => 'Please check you credentials and try again'
]);
}
return redirect()->home();
}
public function show() {
return view('login.show');
}
public function destroy() {
auth()->logout();
return redirect()->home();
//return redirect()->route('home');
}
}
发生的情况是,当我登录并尝试访问/login 时,我被重定向到home,但我在浏览器中收到“抱歉,找不到页面”。我知道中间件会在我尝试登录时检查我是否是访客,如果我不是,它应该将我重定向到推荐页面。
我检查了php artisan route:list,我可以看到/ 被分配了名称home。我现在没有想法,我将不胜感激。
【问题讨论】:
-
您的 PostsController 的索引方法正确吗?
-
@mattQuest 是的,我有。
标签: laravel