php - 登录后 Laravel 路由不起作用

标签 php laravel

我是 Laravel 的新手,刚刚开始构建我的第一个应用程序。我正在使用 Laravel 5.2.5。我正在关注 laracast 视频以实现身份验证。当我第一次启动应用程序时,我可以访问/auth/register、/auth/login 和/auth/logout。注册和登录工作正常。 Register 在我的用户表中创建一个条目,/auth/login 允许我使用该用户登录。成功登录后,我尝试注销。当我尝试访问/auth/logout 时,我收到 NotFoundHttpException。当我尝试访问/auth/login 和/auth/register 时,我遇到了同样的异常。此时我只能访问欢迎和测试页面。在欢迎页面上,我显示了当前登录的用户,我可以看到他仍然处于登录状态。我没有更改其余文件中的任何代码。

这是我的 routes.php 代码:

Route::group(['middleware' => ['web']], function () {
    Route::get('welcome', function () {
        return view('welcome');
    });
    Route::get('test','TestController@index');

    Route::get('auth/login', 'Auth\AuthController@getLogin');
    Route::post('auth/login', 'Auth\AuthController@postLogin');
    Route::get('auth/logout', 'Auth\AuthController@getLogout');

// Registration routes...
    Route::get('auth/register', 'Auth\AuthController@getRegister');
    Route::post('auth/register', 'Auth\AuthController@postRegister');
});

认证 Controller :

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller
{  

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    /**
     * Where to redirect users after login / registration.
     *
     * @var string
     */
    protected $redirectTo = '/welcome';

    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'logout']);
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|confirmed|min:6',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }
}

登录.blade.php:

<form method="POST" action="/auth/login">
    {!! csrf_field() !!}

    <div>
        Email
        <input type="email" name="email" value="{{ old('email') }}">
    </div>

    <div>
        Password
        <input type="password" name="password" id="password">
    </div>

    <div>
        <input type="checkbox" name="remember"> Remember Me
    </div>

    <div>
        <button type="submit">Login</button>
    </div>
</form>

注册.blade.php:

<form method="POST" action="/auth/register">
    {!! csrf_field() !!}

    <div>
        Name
        <input type="text" name="name" value="{{ old('name') }}">
    </div>

    <div>
        Email
        <input type="email" name="email" value="{{ old('email') }}">
    </div>

    <div>
        Password
        <input type="password" name="password">
    </div>

    <div>
        Confirm Password
        <input type="password" name="password_confirmation">
    </div>

    <div>
        <button type="submit">Register</button>
    </div>
</form>

欢迎.blade.php:

            <div class="title">Laravel 5</div>
            <div>{{\Auth::user()}}</div>

最佳答案

长话短说:这是因为您没有注册'/' 路由。

更长的解释:当您尝试访问/auth/login/auth/register 时,我将首先回答您的NotFoundHttpException

在您的 AuthController 中,您将看到这行代码:

public function __construct()
{
    $this->middleware('guest', ['except' => 'logout']);
}

这段代码基本上确保只有 guest (或未经身份验证的用户)才能访问,除非涉及注销方法。

这个“访客”中间件是什么?如果你查看 app/Http/Kernel.php,你会看到这行代码:

'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,

“guest”基本上是 RedirectIfAuthenticated 类的别名。在该类中,您将看到这行代码:

if (Auth::guard($guard)->check()) {
    return redirect('/');
}

您会看到,如果用户已经通过身份验证,他们将被重定向到 '/' 页面。由于您没有注册该路由,您将收到 NotFoundHttpException。您可以在路由文件中注册该路由,也可以在 RedirectIfAuthenticated 类中更改路由。

接下来,当您出于同样的原因尝试注销时,您会遇到同样的异常。当您尝试注销时,它会触发这段代码:

public function logout()
{
    Auth::logout();

    return redirect(property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout : '/');
}

换句话说,您将被重定向到您尚未注册的'/' 页面。如果您想重定向到另一个页面而不是 '/' 页面,请将这行代码添加到您的 AuthController

protected $redirectAfterLogout = '/some-other-route';

关于php - 登录后 Laravel 路由不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34531188/

相关文章:

php - 在 Owncloud 应用程序中回滚 SQL 事务

php - 商店后 Laravel 5 重定向

php - 在 laravel 控制台执行 php artisan migrate 时出错

mysql - 尝试获取非对象的属性 'parameter'

javascript - 如何在ajax响应后在Javascript中按描述进行过滤

PHP 计算数组中的值

php - php5 和 php5-dev 有什么区别

PHP:超出最大执行时间 0

php - Mac 上的代客安装 - 无法重新声明

php - Testing.php 第 39 行中的 FatalThrowableError : Class 'App\TestingModel' not found laravel 5. 3