You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel提交文章评论表单报419 PAGE EXPIRED错误如何解决?

修复步骤

第一步:修复表单CSRF缺失问题

在Blade模板的form标签内部任意位置添加@csrf指令即可,示例修改后的表单代码:

<form method="post" action="{{ route('send-comment') }}">
    @csrf {{-- 新增这一行CSRF令牌生成代码 --}}
    <input type="hidden" name="article_id" value="{{ $article->id }}" />
    <!-- 其余表单原有代码保持不变 -->
    <input class="submit-button" type="submit" value="提交评论">
</form>

第二步:修复控制器变量拼写错误

统一变量名即可,修改后的控制器代码示例:

public function sendComment(Request $request)
{
    $articleComment = new ArticleComment;

    $articleComment->name = $request->get('name');
    $articleComment->email = $request->get('email');
    $articleComment->text = $request->get('text');
    // 修正变量名拼写错误
    $article = Article::find($request->get('article_id'));
    $article->article_comments()->save($articleComment);

    return back();
}

补充建议

可以在控制器方法中增加请求参数校验,避免空值、非法邮箱格式等异常数据入库,示例校验代码:

$validated = $request->validate([
    'name' => 'required|string|max:128',
    'email' => 'required|email|max:128',
    'text' => 'required|string',
    'article_id' => 'required|exists:articles,id'
]);

内容的提问来源于stack exchange,提问作者user16993770

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 05:24:08