I know that one can use $request->get('my_param') or Input::get('my_param') to get a POST or GET request parameter in Laravel (I'm toying with v5/dev version now, but it's the same for 4.2).

But how can I make sure that my my_param came via a POST parameter and was not just from a ?my_param=42 appended to the URL? (besides reverting to the ol' $_POST and $_GET superglobals and throwing testability out the window)

(注意:我还知道,如果POST和URL/GET参数都具有相同的名称,Request::get方法将为我提供POST请求的POST参数,但是.但是如果参数是通过url查询字符串进入的,我想用一种Laravel惯用的方式来了解这一点.)

推荐答案

In the class Illuminate\Http\Request (or actually the Symphony class it extends from Symfony\Component\HttpFoundation\Request) there are two class variables that store request parameters.

public $query - for GET parameters

public $request - for POST parameters

Both are an instance of Symfony\Component\HttpFoundation\ParameterBag which implements a get method.

Here's what you can do (although it's not very pretty)

$request = Request::instance();
$request->request->get('my_param');

Laravel相关问答推荐

警告:构建Reaction App(VITE)后无法解码下载的字体警告

Laravel phpunit 测试失败并出现 PDOException:SQLSTATE[HY000]:一般错误:1 接近0:语法错误

使用Laravel Blade Formatter和PHP Intelephense进行 VSCode 格式化

Laravel Livewire 组件在刷新后不会自动刷新/重新加载

Laravel Eloquent - 随叫随到的加密/解密数据

如何在 Laravel 4 中使用没有 id 的 Eloquent 更新数据

Eloquent 的集合方法,例如 only 或 except 返回一个空集合

致命错误:找不到类App\Http\Controllers\Redirect

Laravel 验证 - 输入必须是数组中的项目之一

Laravel 5.3 Electron邮件队列中不允许序列化关闭

SQLSTATE [01000]:警告:1265 列的数据被截断(truncated)

约定表名(带下划线)

如何使用命令行手动运行 laravel/lumen 作业(job)

Laravel Mail 发送Electron邮件但返回 false

Laravel 迁移 - 删除列

如何在 laravel 用户删除中重置自动增量?

获取多列字段?

Laravel Eloquent 模型属性

如何在没有日志(log)、没有信息的情况下调试 Laravel 错误 500

在 Laravel 中翻译特定语言