dpjjmo3079 2018-04-12 13:28
浏览 44
已采纳

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

I am writing the logic in which the user accepts Terms and Conditions in a separate page and here is the workflow.

  1. The user is logged in
  2. If they have not accepted the latest Terms and Conditions, show them the /welcome page.
  3. If they accept them, then there is a link redirecting them to /accept

I have got the following routes set up:

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

Issue is that when a user is logged in, redirected to the /welcome page instead of them clicking on the Accept button to be redirected to the /accept route then it breaks with the common message Sorry, the page you are looking for could not be found.

I am guessing it's related to middleware. Any ideas?

Update

// 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');
        });

    });
});

and this is the middleware:

class TermsMiddleware
{
    /**
     * @var UserService
     */
    protected $userService;

    /**
     * TermsMiddleware constructor.
     * @param UserService $userService
     */
    public function __construct(UserService $userService)
    {
        $this->userService = $userService;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (!$this->userService->hasAcceptedTerms()) {
            return redirect('/welcome');
        }

        return $next($request);
    }
}
  • 写回答

1条回答 默认 最新

  • dounuo9921 2018-04-12 13:40
    关注
    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 );
        }
    }
    

    add to your HTTP kernel routeMiddleware, 'terms' => YourNewTermsMiddleware::class

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#vscode#的问题:布料滤波算法中C++实现pcl在Vscode中pcl库没有#include <pcl>
  • ¥15 fpga:ov5640采集tft显示
  • ¥20 创建taro项目,在vscode上能够写跨平台写微信小程序代码
  • ¥15 网站出现错误跳转问题
  • ¥15 Docker容器里已经安装了ssh,但打包迁移到新机器一直容器一直提示unrecognized service。
  • ¥15 综合布线实例设计,就好看好看不恐怖可好滤镜好聚
  • ¥15 使用moviepy库视频合并时出错
  • ¥30 FLUENT液固传质UDF
  • ¥15 怎么看梯度直方图以,怎么判断梯度消失/爆炸,怎么解决
  • ¥15 aspnetdll文件访问拒绝