【问题标题】:Is this a good practice on Laravel? [duplicate]这是 Laravel 的一个好习惯吗? [复制]
【发布时间】:2021-03-18 19:18:40
【问题描述】:

我正在学习 Laravel 8 并尝试进行表单验证,我想知道我的 $_POST 是否正确?或者有更好的方法来做到这一点,还是我的代码有问题?

请参阅下面的代码:

<?php
 namespace App\Http\Controllers ;

 use illuminate\Http\Request ;
 


 class customers extends Controller {

    public function getData (Request $request){

        $name = $_POST['name']; // <--- is this right ?

    }
       
 }

【问题讨论】:

  • 你为什么要使用 $_POST ?
  • 真的你验证了他们直接在路由Route::get(...);Route::post(...);等中发送请求的动词
  • 使用$request-&gt;name$request-&gt;input('name')

标签: php html forms security laravel-8


【解决方案1】:

Laravel 具有处理输入数据的内置功能。

https://laravel.com/docs/8.x/requests#input

在你的情况下,你想做:

$name = $request->input('name');

除其他事项外,如果缺少 name 字段,这将保护您免受“未定义索引”错误的影响。

【讨论】:

    【解决方案2】:
    <?php
     namespace App\Http\Controllers;
    
     use Illuminate\Http\Request;
    
    
    
    class customers extends Controller {
    
        public function getData (Request $request){
    
        // $name = $_POST['name']; // <--- this is not a right method to get any value in laravel we have to using Illuminate\Http\Request; for get any post and get value
    
        $name = $request->name;
    
     }
       
    }
    

    这里是检查文档链接doc

    【讨论】:

      猜你喜欢
      • 2015-05-08
      • 1970-01-01
      • 2010-09-11
      • 1970-01-01
      • 1970-01-01
      • 2011-07-17
      • 1970-01-01
      • 2020-11-07
      • 2010-11-12
      相关资源
      最近更新 更多