Laravel view & Response

Source: Internet
Author: User
This article mainly introduces the Laravel Framework's view Response, which is very simple and practical. For more information, see

This article mainly introduces the Laravel Framework's view Response, which is very simple and practical. For more information, see

Basic Response

Returns a string from a route.

The Code is as follows:


Route: get ('/', function ()
{
Return 'Hello world ';
});

Create custom Response

The Response class inherits from the Symfony \ Component \ HttpFoundation \ Response class and provides multiple methods for building HTTP Response.

The Code is as follows:


$ Response = Response: make ($ contents, $ statusCode );

$ Response-> header ('content-type', $ value );

Return $ response;

If you need to access the method of the Response class but want to return a view as the Response content, you can easily implement it by using the Response: view method:

The Code is as follows:


Return Response: view ('hello')-> header ('content-type', $ Type );

Add Cookie in Response

The Code is as follows:


$ Cookie = Cookie: make ('name', 'value ');

Return Response: make ($ content)-> withCookie ($ cookie );

Redirection

Returns a redirection.

Return Redirect: to ('user/login ');
Returns a redirection with data.

Return Redirect: to ('user/login')-> with ('message', 'login failed ');
Note: The with method writes data to the Session and obtains the data through the Session: get method.
Returns a redirection to a named route.

Return Redirect: route ('login ');
Returns a redirection to a named route with parameters.

Return Redirect: route ('profile ', array (1 ));
Returns a redirection route entry with a named parameter.

Return Redirect: route ('profile ', array ('user' => 1 ));
Returns a redirection to the Controller Action.

Return Redirect: action ('homecontroller @ Index ');
Returns a redirection to the Controller Action with Parameters

Return Redirect: action ('usercontroller @ profile ', array (1 ));
Returns a redirection to the Controller Action with a named Parameter

Return Redirect: action ('usercontroller @ profile ', array ('user' => 1 ));

View

A view usually contains HTML code in an application, facilitating the separation of the presentation layer from the Controller and business logic. The view is stored in the app/views directory.

A simple view case:

The Code is as follows:




Hello, <? Php echo $ name;?>


Return the view to the browser using the following method:

The Code is as follows:


Route: get ('/', function ()
{
Return View: make ('greeting ', array ('name' => 'taylor '));
});

The second parameter passed to View: make is an array, which will be passed to the View.

Pass data to view

The Code is as follows:


// Using conventional approach
$ View = View: make ('greeting ')-> with ('name', 'Steve ');

// Using Magic Methods
$ View = View: make ('greeting ')-> withName ('Steve ');

In the above case, the $ name variable is accessible in the view and its value is Steve.

You can also share the same data in all views:

View: share ('name', 'Steve ');

Pass a subview to a view

You may want to add a view to another view. For example, the sub-view stored in the app/views/child/view. php file is passed to another view, as shown below:

The Code is as follows:


$ View = View: make ('greeting ')-> nest ('child', 'Child. view ');

$ View = View: make ('greeting ')-> nest ('child', 'Child. view', $ data );

The child view can be output in the parent View:

The Code is as follows:




Hello!
<? Php echo $ child;?>


View Synthesizer

A view synthesizer can be a callback function or a class method that is called when a view is created. If you want to bind some data to a view every time you create a view in an application, you can use a view synthesizer to organize the code in one place. Therefore, the view synthesizer is like a "view model" or "host ".

Define a view Synthesizer

The Code is as follows:


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

Now, each time you create a profile view, count is bound to the view.

You can also bind a view synthesizer to multiple views at the same time:

The Code is as follows:


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

If you prefer class-based view synthesizer, IoC container can provide more convenience, as shown below:

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

View synthesizer classes are defined as follows:

The Code is as follows:


Class ProfileComposer {

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

}

Note that there is no rule on where the view synthesizer class is stored. Therefore, you can store it as long as you can specify a location in the composer. json file and load it automatically.

View creator

The view creator works in almost the same way as the view synthesizer. The difference is that when a view is instantiated, the view creator is triggered immediately. The view builder can be easily defined using the creator method:

The Code is as follows:


View: creator ('profile ', function ($ 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 '));
Create an object to download Response

Return Response: download ($ pathToFile );

Return Response: download ($ pathToFile, $ status, $ headers );
Note: Symfony HttpFoundation is used to process file downloads. The file name to be downloaded must contain only ASCII characters.

Response macro

If you want to customize a response for reuse in many routing and controllers in your application, you can use the Response: macro method:

The Code is as follows:


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

The macro method accepts two parameters: a specified 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.

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.