【问题标题】:Laravel Request: Process User InputLaravel 请求:处理用户输入
【发布时间】:2021-12-27 21:18:43
【问题描述】:

关于对 Laravel 的用户输入请求的快速问题:

public function store(Request $request)
{
    $name = $request->nameValue;      //Doc: $name = $request->('nameValue');
}

我是否必须按照 Doc 中提到的方式提出所有请求,或者是否也允许“快速”方式?

$request->value 和 $request->('value') 没有区别吗?到目前为止,两者都工作正常 - 但如果我只使用 $request->value,我不希望有任何安全问题。

非常感谢您的帮助:)

【问题讨论】:

  • 你的意思是 $request->input('nameValue')。但是所有方法都很好。
  • 如果你知道你想要一个请求“输入”,我会使用input,如果你使用动态属性,你可以获得输入或路由参数
  • 那么$request->value和$request->('value')没有区别吗?到目前为止,两者都工作正常 - 但如果我使用 $request->value,我不希望有任何安全问题

标签: laravel laravel-request


【解决方案1】:

我认为访问请求输入的不同方法不会有太多性能问题... 无论如何,我将从this answer 中删除这个问题的旧答案。

use Illuminate\Http\Request;

get() 和 input() 是不同类的方法。第一个是 Symfony HttpFoundation Request 的方法,input() 是 扩展 Symfony Request 类的 Laravel Request 类。

所以,我认为更好的是input() 方法。

【讨论】:

    【解决方案2】:

    在 laravel 上,有一个名为 Illuminate\Http\Request 的特定类,它提供了一种面向对象的方式来与客户端发送的 HTTP 请求进行交互

    • 访问请求(两者访问数据的速度相同)
    $name = $request->input('name');
    $name = $request->name;
    
    • 依赖注入和路由参数
    use App\Http\Controllers\UserController; // call the controller
    
    Route::put('/user/{id}', [UserController::class, 'update']); // set a slug as a parameter on routes
    
    public function update(Request $request, $id)
    {
       return $id; // access the parameter by contoller
    }
    
    
    • 检索请求路径
    $uri = $request->path(); // will fetch the path 
    
    
    • 检索请求方法
    $method = $request->method(); // will fetch the method of request EG: GET / POST / PUT / DESTROY
    
    

    更多信息请查看官方文档LARAVEL

    【讨论】:

    • 所以$request->value和$request->('value')没有区别
    • 这两个输入请求没有区别。您可以使用 laravel 调试栏检查进度和性能https://github.com/barryvdh/laravel-debugbar
    • 非常感谢 :)
    • 确保给答案出票
    猜你喜欢
    • 1970-01-01
    • 2016-04-21
    • 2019-09-26
    • 2014-08-26
    • 2017-03-01
    • 2019-02-08
    • 1970-01-01
    • 2018-07-24
    • 1970-01-01
    相关资源
    最近更新 更多