zoukankan      html  css  js  c++  java
  • laravel中的表单请求类型和CSRF防护(六)

    laravel中为我们提供了绑定不同http请求类型的函数。

    Route::get('/test', function () {});
    Route::post('/test', function () {});
    Route::put('/test', function () {});
    Route::patch('/test', function () {});
    Route::delete('/test', function () {});
    Route::options('/test', function () {});
    

    但有些时候,我们通过创建资源控制器,里面的 update() 方法绑定的是 PUT 类型的http请求。

    这就需要我们通过表单提交模拟PUT请求。我们可以自已添加一个 _method 的隐藏字段,值为 PUT。

    <form action="{{ route('test') }}" method="post">
        <input type="hidden" name="_method" value="PUT">
        用户名:<input type="text" name="name">
        密码:<input type="password" name="pwd">
        <input type="submit" value="提交">
    </form>
    

    也可以使用laravel为我们提供的 method_field() 方法。

    <form action="{{ route('test') }}" method="post">
        {{ method_field('PUT') }}
        用户名:<input type="text" name="name">
        密码:<input type="password" name="pwd">
        <input type="submit" value="提交">
    </form>
    

    laravel默认会对每个提交请求,进行csrf令牌的验证。为了通过验证,需要在表单中添加 _token 隐藏字段。

    <form action="{{ route('test') }}" method="post">
        <input type="hidden" name="_token" value="{{ csrf_token() }}">
        用户名:<input type="text" name="name">
        密码:<input type="password" name="pwd">
        <input type="submit" value="提交">
    </form>
    

    或者使用 csrf_field() 方法。

    <form action="{{ route('test') }}" method="post">
        {{ csrf_field() }}
        用户名:<input type="text" name="name">
        密码:<input type="password" name="pwd">
        <input type="submit" value="提交">
    </form>
    
  • 相关阅读:
    数据结构
    ADC
    SPI
    定时器原理
    IO中断
    恩智浦样片申请
    UART
    随机生成数字验证码
    判断网络是否连接Internet
    清理SQL数据库日志
  • 原文地址:https://www.cnblogs.com/jkko123/p/10805728.html
Copyright © 2011-2022 走看看