【问题标题】:Redirecting to named route using middleware yields page not found使用中间件重定向到命名路由会产生找不到页面
【发布时间】: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


【解决方案1】:

试试这个

return redirect()->route('home');

【讨论】:

  • 虽然你只提到了这个片段,但我想它不会有任何影响,因为问题与构造函数有关,中间件将处理正在重定向的页面。感谢您的关注。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-10
  • 2018-11-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多