PHP经验分享laravel实战笔记

路由之POST请求(三)

2019-10-14  本文已影响0人  寒云暮雨

这一次我们讲POST请求
post请求和get请求的定义方式一样,只不过在laravel中为了安全,post请求会有csrf限制
老规矩,上代码

<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class IndexController extends Controller
{
    //
    public function index()
    {
        return view('welcome');
    }

    public function add(Request $request)
    {
        return $request->all();
    }
}

路由

<?php

Route::get('/', 'IndexController@index');
Route::post('/add', 'IndexController@add');

我们定义了一个post请求,在命令行执行,起一个web服务

php artisan serve

我们用post进行模拟post请求


post请求测试

服务端返回419页面,遇到这样的情况怎么处理呢?
1、解除当前路由的限制
修改learnLaravel\app\Http\Middleware\VerifyCsrfToken.php文件中的代码,将路由/add加入到$except数组中,表示这个路由不受csrf防护

<?php

namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;

class VerifyCsrfToken extends Middleware
{
    /**
     * Indicates whether the XSRF-TOKEN cookie should be set on the response.
     *
     * @var bool
     */
    protected $addHttpCookie = true;

    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        //
        '/add'
    ];
}

我们再次用postman模拟post请求,结果如下图


image.png

2、增加token验证
新建页面,内容如下

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>Laravel</title>

        <!-- Fonts -->
        <link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet">

        <!-- Styles -->
    </head>
    <body>
    <form action="{{url('/add')}}" method="post">
        @csrf
        <input type="submit">
    </form>
    </body>
</html>

查看页面源码


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Laravel</title>

    <!-- Fonts -->
    <link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet">

    <!-- Styles -->
</head>
<body>
<form action="http://127.0.0.1:8000/add" method="post">
    <input type="hidden" name="_token" value="F8eL5FrA5scHvqR0SAyQoaNEbL1bLLNyqetjNYcJ">        <input type="submit">
</form>
</body>
</html>

我们发现@csrf被转化成了

    <input type="hidden" name="_token" value="F8eL5FrA5scHvqR0SAyQoaNEbL1bLLNyqetjNYcJ">      

我们再次修改F:\web\learnLaravel\app\Http\Middleware\VerifyCsrfToken.php中的文件移除$except数组中的/add

<?php

namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;

class VerifyCsrfToken extends Middleware
{
    /**
     * Indicates whether the XSRF-TOKEN cookie should be set on the response.
     *
     * @var bool
     */
    protected $addHttpCookie = true;

    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        //
    ];
}


打开我们的页面点击提交表单,数据正常提交,419限制也没有了,至此我们post请求常见问题讲解完毕。

上一篇下一篇

猜你喜欢

热点阅读