首页 > 解决方案 > “页面已过期错误 419” - Laravel 7

问题描述

我的注册页面正在正确显示表单,其中 CsrfToken ({{ csrf_field() }}) 出现在表单中。

            <form method="post" action="{{route('products.store')}}">
            {{csrf_field()}}
            {{ method_field('post') }}

            <div class="form-group">
                <label for="exampleName">Name</label>
                <input type="text" class="form-control" name="name" id="exampleName" placeholder="Enter Name" aria-describedby="emailHelp">
            </div>
            <div class="form-group">
                <label for="exampleDescription">Description</label>
                <input type="text" class="form-control" name="desc" id="exampleDescription" placeholder="Enter Name" aria-describedby="emailHelp">
            </div>
            <div class="form-group">
                <label for="exampleWeight">Weight</label>
                <input type="text" class="form-control" name="weight" id="exampleWeight" aria-describedby="emailHelp">
            </div>
            <div class="form-group">
                <label for="examplePrice">Price</label>
                <input type="text" class="form-control" name="price" id="examplePrice" aria-describedby="emailHelp">
            </div>

            <button type="submit" class="btn btn-success">Submit</button>
        </form>

当我提交表单时(也刚刚重新加载),它给出了页面已过期,由于错误 419。请刷新并重试。错误。

我非常尝试但无法解决

请帮助我,非常感谢

标签: laravel

解决方案


本文可能对您有所帮助: https ://medium.com/@tiwarishani/how-to-solve-the-page-expired-419-error-in-laravel-69fd97b0aa71

1. 你在生产吗?使用https?我有同样的问题,不是 CSRF 令牌。所以我为此更改了 AppServiceProvider.php boot() 方法(在我的例子中之前是空的):

public function boot(UrlGenerator $url)
{
    Schema::defaultStringLength(191);
    if (env('APP_ENV') !== 'local') {
        $url->forceScheme('https');
    }
}

不要忘记顶部的这一行:

use Illuminate\Routing\UrlGenerator;

** 关于在配置文件之外使用 env(),请务必执行

php artisan config:clear

测试前

2.您需要对 storage/framework/sessions 文件夹(755)执行写入权限

chmod 755 storage/framework/sessions
chmod 755 bootstrap/cache
chmod 644 vendor (not very sure about this)

SESSION_DOMAIN 环境变量可能很重要。


推荐阅读