Laravel路线:在auth中间件中击中路线时断开

时间:2018-04-12 13:28:35

标签: php laravel authentication middleware

我正在编写用户在单独的页面中接受条款和条件的逻辑,这是工作流程。

  1. 用户已登录
  2. 如果他们尚未接受最新的条款及条件,请向他们显示/welcome页。
  3. 如果他们接受了这些内容,则会有一个链接将其重定向到/accept
  4. 我已设置以下路线:

    Auth::routes();
    
    // Temp
    Route::get('/', function () {
        return redirect('/login');
    });
    
    Route::get('/logout', 'Auth\LoginController@logout');
    
    Route::post('password/reset/{token}', 'Auth\ResetPasswordController@postReset')->name('password.request');
    
    // Auth middleware
    Route::group(['middleware' => ['auth']], function () {
        // Temp
        Route::get('/discovery', function () {
            return view('pages.discovery');
        });
    
        // Temp
        Route::get('/welcome', function () {
            return view('pages.welcome');
        });
    
        Route::get('/accept', 'Auth\LoginController@accept');
    });
    

    问题是当用户登录时,重定向到/welcome页面而不是他们点击“接受”按钮以重定向到/accept路由,然后它会中断公共消息{{ 1}}

    我猜它与中间件有关。有什么想法吗?

    更新

    Sorry, the page you are looking for could not be found.

    这是中间件:

    // Auth middleware
    Route::group(['middleware' => ['auth']], function () {
    
        Route::get('/accept', 'Auth\LoginController@accept')->name('terms.accept');
        Route::get('/welcome', function () {
            return view('pages.welcome');
        });
        Route::group(['middleware' => ['terms']], function () {
            Route::get('/discovery', function () {
                return view('pages.discovery');
            });
    
        });
    });
    

1 个答案:

答案 0 :(得分:1)

Auth::routes();

Route::get('/', function () {
    return redirect('/login');
});

Route::get('/logout', 'Auth\LoginController@logout');
Route::post('password/reset/{token}', 'Auth\ResetPasswordController@postReset')->name('password.request');

Route::group(['middleware' => ['auth']], function () {
    Route::get('/accept', 'Auth\LoginController@accept')->name('get.accept');
    Route::group(['middleware' => ['terms']], function () {
        Route::get('/discovery', function () {
            return view('pages.discovery');
        });
        Route::get('/welcome', function () {
            return view('pages.welcome');
        });
    });
});

php artisan make:middlware Terms

<?php

namespace YourNamespace;

use Closure;

class Terms
{

    public function handle( $request, Closure $next, $guard = null )
    {
        if( ! $request->user()->accepts_terms ) {
            return redirect( route( 'get.accept' ) );
        }
        return $next( $request );
    }
}

添加到您的HTTP内核routeMiddleware,'terms'=&gt; YourNewTermsMiddleware ::类