This article mainly introduces the Laravel framework View & amp; Response, which is very simple and practical. if you need it, refer
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.