Laravel response and view

Source: Internet
Author: User
Laravel response and view basic response

// Directly responds to the string
Route: get ('example/test20', function (){
Return 'Hello World ';
});
// Customize HTTP response
Route: get ('example/test21', function (){
Return Response: make ('content does not exist', 404 );
});
// Response View
Route: get ('example/test22', function (){
Return Response: view ('test22 ');
});
// Add a cookie to the response
Route: get ('example/test23', function (){
Return Response: view ('test22')-> withCookie (Cookie: make ('key', 'This is value '));

Response redirection

// Response redirection
Route: get ('example/test24', function (){
Return Redirect: to ('example/test21')-> with ('username', 'xiaoming ');
});
// Redirect with data
Route: get ('example/test25', function (){
// The with method writes the data to the Session and obtains the data through the Session: get method.
Return Redirect: to ('example/test21')-> with ('username', 'xiaoming ');
});
// Redirect to named route
Return Redirect: route ('login ');
// The route entry with the specified name parameter.
Return Redirect: route ('Profile ', array ('user' => 1 ));
// Redirect to the specified controller method
Return Redirect: action ('homecontroller @ index ');
// Redirect to the specified controller method with parameters
Return Redirect: action ('usercontroller @ profile ', array ('user' => 1 ));

Response view

// Response View and pass parameters
Route: get ('example/test30', function (){
// Method 1
Return View: make ('test30', array ('name' => 'xiaoming '));
// Method 2
// Return View: make ('test30')-> with ('name', 'xiaoming2 ');
// Method 3
// Return View: make ('test30')-> withName ('xiaoming3 ');
// Method 4. note: share the same data in all views
// View: share ('name', 'Steve ');
});
// Input the subview in the view
Route: get ('example/test31', function (){
$ Data = array ('name' => 'John ');
// Place the sub-view in app/views/child/child_view.php. you can also pass variables to the sub-view.
Return View: make ('test30')-> with ($ data)-> nest ('child ', 'Child. child_view', $ data );
});

View component or view synthesizer

If you want to bind the specified data when creating a view, you can define the View component:

View: composer ('Profile ', function ($ view)
{
$ View-> with ('count', User: count ());
});

Add multiple views to the view component:

View: composer (array ('Profile ', 'dashboard'), function ($ view)
{
$ View-> with ('count', User: count ());
});

If you use a class-based view component:

View: composer ('Profile ', 'profilecomposer ');

The view component class is created as follows:

Class ProfileComposer {
Public function compose ($ view)
{
$ View-> with ('count', User: count ());
}
}

Special Response

// Create a JSON response
Return Response: json (array ('name' => 'Steve ', 'state' => 'CA '));
// Create a JSONP response
Return Response: json (array ('name' => 'Steve ', 'state' => 'CA')-> setCallback (Input :: get ('callback '));
// File download response
Return Response: download ($ pathToFile );
Return Response: download ($ pathToFile, $ name, $ headers );
Note: the Symfony HttpFoundation class library for file download management requires the file name to be ASCII encoded.

Response macro, use Response: macro to customize the Response

Response: macro ('caps', function ($ value)
{
Return Response: make (strtoupper ($ value ));
});

The macro method accepts two parameters: a macro name and a closure. When the macro name is called through the Response class, the closure will be executed:

Return Response: caps ('Foo ');

You can define the macro in the file in the app/start directory. Alternatively, you can organize your macros through a separate file and include the macro into a start file.

Reprinted on http://www.phpddt.com/php/laravel-response.html

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.