Input

本文介绍了如何使用Input类处理用户输入的数据,包括获取输入值、验证输入存在性、获取所有输入及部分输入等操作。同时详细说明了如何处理文件上传,如检查文件是否存在、验证文件有效性、移动上传文件及获取文件相关信息。

Input

Please note that both Input and Request do NOT sanitize your data, it is up to you to do that.

Basic Input

You may access all user input with a few simple methods. You do not need to worry about the HTTP verb used for the request, as input is accessed in the same way for all verbs.

Retrieving An Input Value
$name = Input::get('name');
Retrieving A Default Value If The Input Value Is Absent
$name = Input::get('name', 'Sally');
Determining If An Input Value Is Present
if (Input::has('name')) {  // }
Getting All Input For The Request
$input = Input::all();
Getting Only Some Of The Request Input
$input = Input::only('username', 'password'); $input = Input::except('credit_card');

When working on forms with "array" inputs, you may use dot notation to access the arrays:

$input = Input::get('products.0.name');

Note: Some JavaScript libraries such as Backbone may send input to the application as JSON. You may access this data via Input::get like normal.

Old Input

You may need to keep input from one request until the next request. For example, you may need to re-populate a form after checking it for validation errors.

Flashing Input To The Session
Input::flash();
Flashing Only Some Input To The Session
Input::flashOnly('username', 'email'); Input::flashExcept('password');

Since you often will want to flash input in association with a redirect to the previous page, you may easily chain input flashing onto a redirect.

return Redirect::to('form')->withInput(); return Redirect::to('form')->withInput(Input::except('password'));

Note: You may flash other data across requests using the Session class.

Retrieving Old Data
Input::old('username');

Files

Retrieving An Uploaded File
$file = Input::file('photo');
Determining If A File Was Uploaded
if (Input::hasFile('photo')) {  // }

The object returned by the file method is an instance of the Symfony\Component\HttpFoundation\File\UploadedFile class, which extends the PHP SplFileInfo class and provides a variety of methods for interacting with the file.

Determining If An Uploaded File Is Valid
if (Input::file('photo')->isValid()) {  // }
Moving An Uploaded File
Input::file('photo')->move($destinationPath); Input::file('photo')->move($destinationPath, $fileName);
Retrieving The Path To An Uploaded File
$path = Input::file('photo')->getRealPath();
Retrieving The Original Name Of An Uploaded File
$name = Input::file('photo')->getClientOriginalName();
Retrieving The Extension Of An Uploaded File
$extension = Input::file('photo')->getClientOriginalExtension();
Retrieving The Size Of An Uploaded File
$size = Input::file('photo')->getSize();
Retrieving The MIME Type Of An Uploaded File
$mime = Input::file('photo')->getMimeType();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值