您可以通过几个简单的方法访问所有用户输入。使用 Input facade 时,您无需担心请求的 HTTP 动词,因为所有动词的输入都以相同的方式访问。全局 input 辅助函数 是 Input::get 的别名。
$name = Input::get('name');
$name = input('name');$name = Input::get('name', 'Sally');if (Input::has('name')) {
//
}$input = Input::all();$input = Input::only('username', 'password');
$input = Input::except('credit_card');当处理包含“数组”输入的表单时,可以使用点表示法来访问这些数组:
$input = Input::get('products.0.name');一些 JavaScript 库(例如 Backbone)可能会以 JSON 格式向应用程序发送输入。您可以像往常一样通过
Input::get访问此数据。
默认情况下,October CMS 创建的所有 cookie 都经过加密并使用身份验证码进行签名,这意味着如果它们被客户端更改,将被视为无效。在 system.unencrypt_cookies 配置键中命名的 cookie 将不会被加密。
$value = Cookie::get('name');$response = Response::make('Hello World');
$response->withCookie(Cookie::make('name', 'value', $minutes));如果您想在响应创建之前设置 cookie,请使用 Cookie::queue 方法。该 cookie 将自动附加到您的应用程序的最终响应中。
Cookie::queue($name, $value, $minutes);$cookie = Cookie::forever('name', 'value');如果你不想某些 Cookie 被加密或解密,你可以在配置中指定它们。这很有用,例如,当你希望通过 Cookie 在前端和服务器端后端之间传递数据时,反之亦然。
将不应加密或解密的 Cookie 名称添加到 config/system.php 配置文件中的 unencrypt_cookies 参数。
'unencrypt_cookies' => [
'my_cookie',
],或者,对于插件,您也可以从您的插件的 Plugin.php 中动态添加这些。
public function boot()
{
Config::push('system.unencrypt_cookies', 'my_cookie');
}您可能需要保留来自一个请求的输入,直到下一个请求。例如,您可能需要在检查表单是否存在验证错误后重新填充它。
Input::flash();Input::flashOnly('username', 'email');
Input::flashExcept('password');由于你经常会希望将输入与重定向到上一页相关联地闪存,你可以轻松地将输入闪存链式地附加到重定向上。
return Redirect::to('form')->withInput();
return Redirect::to('form')->withInput(Input::except('password'));你可以使用 会话 类在请求之间暂存其他数据。
获取单个输入值。
Input::old('username');获取所有旧输入值。
$data = Input::old();你可以从请求中通过 Input 门面上的 file 方法或全局 files() 辅助函数检索 上传的文件。
$file = Input::file('photo');
$file = files('photo');要确定请求中是否存在文件,请使用 hasFile 方法。
if (Input::hasFile('photo')) {
//
}除了检查文件是否存在,你还可以通过 isValid 方法验证文件上传过程中没有出现问题。
if ($file->isValid()) {
//
}返回的文件对象具有与上传文件相关的多种方法。
| Method Name | Purpose |
|---|---|
| move($destinationPath, $fileName) | Moves the uploaded file to a local path |
| store($folder, $disk) | Stores the file using the storage service. |
| storeAs($folder, $name, $disk) | Stores the file with a specific name using the storage service. |
| extension() | Guesses the extension based on the file contents |
| getRealPath() | Returns the local path |
| getClientOriginalName() | Returns the original name |
| getClientOriginalExtension() | Returns the original extension |
| getSize() | Returns the size |
| getMimeType() | Returns the MIME type |
一个移动上传文件的示例。
$file->move($destinationPath);
$file->move($destinationPath, $fileName);示例 存储上传文件 到文件夹中(第一个参数)和一个可选的磁盘(第二个参数)。
$file->store($folder);
$file->store($folder, 's3');文件夹名称
media、resources、uploads或public为保留名称。
使用 storeAs 将文件存储为自定义磁盘名称(第二个参数)的示例。 storePubliclyAs 以公共可见性存储文件。
$file->storeAs($folder, 'avatar');
$file->storeAs($folder, 'avatar', 's3');
$file->storePublicly($folder, 's3');
$file->storePubliclyAs($folder, 'avatar', 's3');一个获取上传文件路径的示例。
$path = $file->getRealPath();一个获取上传文件原始名称的示例。
$name = $file->getClientOriginalName();获取上传文件的扩展名。
$extension = $file->getClientOriginalExtension();获取已上传文件的大小。
$size = $file->getSize();获取上传文件的MIME类型。
$mime = $file->getMimeType();Request 类提供了许多方法,用于检查针对你的应用程序的 HTTP 请求,并扩展了 Symfony\Component\HttpFoundation\Request 类。以下是其中一些亮点。
$uri = Request::path();$method = Request::method();
if (Request::isMethod('post')) {
//
}if (Request::is('admin/*')) {
//
}$url = Request::url();$segment = Request::segment(1);$value = Request::header('Content-Type');$value = Request::server('PATH_INFO');if (Request::secure()) {
//
}if (Request::ajax()) {
//
}if (Request::isJson()) {
//
}if (Request::wantsJson()) {
//
}Request::format 方法将根据 HTTP Accept header 返回请求的响应格式:
if (Request::format() == 'json') {
//
}