Follow me to learn Laravel's request and input _php instance

Source: Internet
Author: User

Basic input

Laravel uses an easy way to access the information submitted by the user. You can access the information submitted by the user in a unified way without worrying about how the user submits the information.

Get a user-submitted value

Copy Code code as follows:

$name = input::get (' name ');

Specify a default return value for user submissions (if the user is not committed)

Copy Code code as follows:

$name = input::get (' name ', ' Sally ');

Determines whether the specified commit information exists

Copy Code code as follows:

if (Input::has (' name '))
{
//
}

Get information submitted by all users

Copy Code code as follows:

$input = Input::all ();

Gets the specified information, or gets all of the submit information that excludes the specified number of commits

Copy Code code as follows:

$input = input::only (' username ', ' password ');

$input = input::except (' Credit_card ');

If the submitted form contains input in an array form, you can access the array using the dot notation:

Copy Code code as follows:

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

Note: There are JavaScript libraries, such as backbone, that submit information in JSON format. Access to information through Input::get, no difference in use.

Cookies

Laravel encrypts all created cookie information, plus the authorization code, which is discarded when the client modifies cookie information without authorization, guaranteeing security.

Gets a specified cookie value

Copy Code code as follows:

$value = cookie::get (' name ');

Add a new cookie key value pair

Copy Code code as follows:

$response = Response::make (' Hello world ');

$response->withcookie (cookie::make (' name ', ' value ', $minutes));

Join the next response cookie queue

If you want to set up cookies before response is created, you can use the Cookie::queue () method. Cookies are automatically added to the final response through the application framework.

Copy Code code as follows:

Cookie::queue ($name, $value, $minutes);

Create a cookie key value pair that never expires

Copy Code code as follows:

$cookie = cookie::forever (' name ', ' value ');

Persistence of user submission information

It is sometimes possible to persist user-submitted information between multiple requests from a user. For example, the user's input is restored when the user submits the information validation failure to return to the submit information page.

Save user submitted information in session

Copy Code code as follows:

Input::flash ();

The information submitted by the specified user is stored in session

Copy Code code as follows:

Input::flashonly (' username ', ' email ');

Input::flashexcept (' password ');


If you need to relate persistent user-submitted information to the operation and redirection operations, you can use the following chain-calling method:

Copy Code code as follows:

return redirect::to (' form ')->withinput ();

return redirect::to (' form ')->withinput (input::except (' password '));

Note: If you want to persist other information, refer to the session class.
To obtain information submitted by a persisted user

Copy Code code as follows:

Input::old (' username ');

File Upload

Get files uploaded by user

Copy Code code as follows:

$file = input::file (' photo ');

Determine if the specified file has been uploaded

Copy Code code as follows:

if (Input::hasfile (' photo '))
{
//
}

The file method returns an instance of the Symfony\component\httpfoundation\file\uploadedfile class that inherits from the PHP Splfileinfo class and provides a way to manipulate the files that the user uploads.

Move a file that has already been uploaded

Copy Code code as follows:

Input::file (' photo ')->move ($destinationPath);

Input::file (' photo ')->move ($destinationPath, $fileName);

Get an uploaded file in the real path of the server

Copy Code code as follows:

$path = input::file (' photo ')->getrealpath ();

Gets the size of a file that has been uploaded

Copy Code code as follows:

$size = input::file (' photo ')->getsize ();

Gets the MIME type of a file that has been uploaded

Copy Code code as follows:

$mime = input::file (' photo ')->getmimetype ();

User requests for more information

The request class provides a number of methods for obtaining detailed information about the request, which inherits from the Symfony\component\httpfoundation\request class. Several representative methods are provided below:

Get request URI

$uri = Request::p ath ();
Determines whether the request path conforms to the specified pattern

Copy Code code as follows:

if (Request::is (' admin/* '))
{
//
}

Get Request URL

$url = Request::url ();
Get request URI Information

$segment = request::segment (1);
Get the Content-type information in the request header

$value = Request::header (' Content-type ');
Gets the value specified in the $_server array

$value = Request::server (' path_info ');
Determine if AJAX requests are used

Copy Code code as follows:

if (Request::ajax ())
{
//
}

To determine whether a request uses HTTPS connections

Copy Code code as follows:

if (Request::secure ())
{
//
}

To detect the response format of a request

The Request::format method returns the response format that the client wants to obtain based on the Accept information of the HTTP request header:

Copy Code code as follows:

if (request::format () = = ' json ')
{
//
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.