登录后Laravel路由不起作用

迪米塔尔·斯帕索夫斯基(Dimitar Spasovski)

我是Laravel的新手,刚刚开始构建我的第一个应用程序。我正在使用Laravel 5.2.5。我正在观看laracast视频,以实现身份验证。首次启动应用程序时,我可以访问/ auth / register,/ auth / login和/ auth / logout。注册和登录工作正常。Register在我的User表中创建一个条目,/ 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');
});

AuthenticationController:

<?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']),
        ]);
    }
}

login.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>

register.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>

welcome.blade.php:

            <div class="title">Laravel 5</div>
            <div>{{\Auth::user()}}</div>
托马斯·金

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

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

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

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

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

什么是“来宾”中间件?如果您向内看app/Http/Kernel.php,您将看到以下代码行:

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

“来宾”基本上是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';

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章