doulian1852 2017-02-15 12:18
浏览 82

Laravel Auth中间件不适用于POST路由

I have a simple form in my site and I want to check if the user is currently logged in before he can post the form. I have put auth middleware in the post route...

Route::post('test','SellController@test_mid')->middleware('auth');

My auth middleware...

<?php

public function handle($request, Closure $next, $guard = null)
{
    if (Auth::guard($guard)->guest()) {
        if ($request->ajax() || $request->wantsJson()) {
            return response('Unauthorized.', 401);
        } else {
            return redirect()->guest('login');
        }
    }

    return $next($request);
}

Here 'login' redirects to my login page.

The problem is after I hit the login button, the error is...

MethodNotAllowedHttpException in RouteCollection.php line 219: at RouteCollection->methodNotAllowed(array('POST')) in RouteCollection.php line 206

Form...

{{ Form::open(array('url'=>"/test")) }}=
    <input type="submit" value="submit">
{{ Form::close() }}

But if I set the auth middleware on a GET route, it works fine. In case of a GET, if I am logged out, it leads me to the login page. After I provide login credentials and hit the login button it leads me to the desired GET route.

  • 写回答

2条回答 默认 最新

  • douxiangui5011 2017-02-15 12:51
    关注

    Make sure your view form uses POST method.

    {{Form::open(array('url'=>'/test', 'method'=>'post'))}}
    

    Please note that $guard variable is NULL by default unless specified. See your /config/auth.php configuration for available guards.

    Route::post('test', 'SellController@test_mid')->middleware('auth:web'); 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100