您可以通过几种简单方法访问所有用户输入。您在使用 Input 门面时,无需担心请求的 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 名称添加至 unencrypt_cookies 参数位于 config/system.php 配置文件中。
'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 头返回请求的响应格式:
if (Request::format() == 'json') {
//
}