【问题标题】:Laravel route [list.remove] not definedLaravel 路由 [list.remove] 未定义
【发布时间】:2019-05-03 03:15:39
【问题描述】:

在我看来(list.blade.php)我有(下面只显示相关部分)

<?php $counter=0; ?>
@foreach($items as $item)
    <button type="button" class="addButton btn btn-default" onclick="document.getElementById({{$counter}}).submit()">
    <i class="fa fa-close"></i>
    <form id="{{$counter}}" action="{{ URL::route('list.remove') }}" method="POST" style="display: none;">
    <input type="hidden" name="item" value="{{$item->item}}"></input>
    @csrf
</form>
<?php $counter++;?>
@endforeach

web.php中的相关控制器:

Route::post('/list/remove', 'listController@removeItem')->name('list.remove')->middleware('auth');

我在listController.php中的函数(目前只是一个测试函数):

public function removeItem(){
  echo "hello";
}

当您单击 list.blade.php 中的按钮时,您会被发送到显示

的页面

错误 419 抱歉,您的会话已过期。请刷新并尝试 再次。

php artisan route:list 返回:

【问题讨论】:

    标签: php laravel routes


    【解决方案1】:

    您的 419 是由于您没有为您的表单使用 CSRF 保护。

    你可以使用@csrf,它会创建一个隐藏的输入,Laravel 使用它来帮助保护表单输入

    <form method="POST" action="/profile">
    @csrf
    ...
    

    有关 CSRF 的更多信息:https://laravel.com/docs/5.7/csrf

    编辑:尝试使用此代码

    <?php $counter=0; ?>
    @foreach($items as $item)
    <i class="fa fa-close"></i>
    <form id="{{$counter}}" action="{{ URL::route('list.remove') }}" method="POST" style="display: none;">
    <input type="hidden" name="item" value="{{$item->item}}">
    @csrf
    <button type="button" class="addButton btn btn-default" onclick="document.getElementById({{$counter}}).submit()">
    </form>
    <?php $counter++;?>
    @endforeach
    

    【讨论】:

    • 我确实使用它。它在输入标签下方
    • 你能用你的命令行做一个:php artisan route:list
    • 另外,将public function removeItem(){ echo "hello"; } 更改为public function removeItem(){ // dd means die and dump dd('hello'); }
    • 将“php artisan route:list”的输出添加到post
    • 也编辑了我的原始帖子
    猜你喜欢
    • 2018-06-30
    • 2018-12-16
    • 2020-02-06
    • 2015-01-16
    • 2019-05-12
    • 1970-01-01
    • 2020-08-16
    • 2019-09-10
    相关资源
    最近更新 更多