首页 文章

Laravel 5.3表格重定向到帖子登录

提问于
浏览
0

我有一个表格应该发送到页面的 OrgController@store . 发生的事情似乎是重定向到/并且永远不会进入OrgController中的我的商店功能 .

根据我在Firebug中的网络选项卡,然后POSTS to / org / add会因某种原因发出GET请求登录,即使我注释掉我对Auth Middleware的调用 .

GET请求登录后会看到我已登录,然后将我重定向到Laravel Auth RedirectIfAuthenticated设置为的位置 . (在这种情况下是/) .

基本上我希望我的路由尊重auth,但也要POST到我的表单 .

我的路线看起来像这样 .

Route::group(['prefix' => 'org'], function () {
    Route::get('search', 'OrgController@search');
    Route::get('browse', 'OrgController@browse');
    Route::get('add', 'OrgController@add');
    Route::post('add', 'OrgController@store');
});

我的表格看起来像这样 .

<form method="post" action="">
       <div class="form-group">
            <label for="parent_org">Parent Organization</label>
            <select class="form-control" id="parent_org" required="true">
                        <option>Org Unit 1</option>
                        <option>Org Unit 2</option>
                        <option>Org Unit 3</option>
                        <option>Org Unit 4</option>
             </select>
        </div>
        <div class="form-group">
             <label for="org_name">Org Unit Name</label>
             <input type="text" class="form-control" id="org_name" required="true">
        </div>
        <div class="checkbox">
            <label><input type="checkbox">Will users be added directly to this organization unit?</label>
        </div>
        <button type="submit" class="btn btn-primary">Add Org</button>
 </form>

我的 php artisan route:list 显示以下内容 .

|        | GET|HEAD | org                    |       | App\Http\Controllers\OrgController@search                              | web,eventlog,auth  |
|        | POST     | org/add                |       | App\Http\Controllers\OrgController@store                               | web,eventlog,auth  |
|        | GET|HEAD | org/add                |       | App\Http\Controllers\OrgController@add                                 | web,eventlog,auth  |
|        | GET|HEAD | org/browse             |       | App\Http\Controllers\OrgController@browse                              | web,eventlog,auth  |
|        | GET|HEAD | org/search             |       | App\Http\Controllers\OrgController@search                              | web,eventlog,auth  |

我正在使用一个自定义Auth提供程序,扩展了Auth中内置的Laravel .

提前致谢

2 回答

  • 1

    Woops .

    问题是我忘了我放入了我的异常处理程序 .

    if ($exception instanceof TokenMismatchException) {
                return redirect('/login');
       }
    

    我的表格中也没有我的CSRF令牌 . 因此它抑制了无效的CSRF令牌错误并且还重定向到登录 .

    .. 学过的知识 .

  • 1

    出于安全目的,请勿在登录页面或任何具有发布请求的URL中禁用令牌 . 只需在表单字段中添加 {{csrf_token()}} 即可 . csrf_token返回带有标记的输入隐藏字段 . 它保护您的表单免受外部滥用攻击 .

    <form method="post" action="">
               <div class="form-group">
                    <label for="parent_org">Parent Organization</label>
          Add csrf_token anywhere inside your form
                 {{csrf_token()}}
                    <select class="form-control" id="parent_org" required="true">
                                <option>Org Unit 1</option>
                                <option>Org Unit 2</option>
                                <option>Org Unit 3</option>
                                <option>Org Unit 4</option>
                     </select>
                </div>
                <div class="form-group">
                     <label for="org_name">Org Unit Name</label>
                     <input type="text" class="form-control" id="org_name" required="true">
                </div>
                <div class="checkbox">
                    <label><input type="checkbox">Will users be added directly to this organization unit?</label>
                </div>
                <button type="submit" class="btn btn-primary">Add Org</button>
         </form>
    

相关问题