Follow me to learn Laravel view & response_php Example

Source: Internet
Author: User
Tags closure

Basic response

Returning a string from the route

Copy Code code as follows:

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

Create a custom response

The Response class inherits from the Symfony\component\httpfoundation\response class and provides several ways to build HTTP Response.

Copy Code code as follows:

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

$response->header (' Content-type ', $value);

return $response;

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

Copy Code code as follows:

return Response::view (' Hello ')->header (' Content-type ', $type);

Add cookies to Response

Copy Code code as follows:

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

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

redirect

Returns a redirect

Return redirect::to (' User/login ');
Returns a redirect with data

Return redirect::to (' User/login ')->with (' message ', ' login Failed ');
Note: The With method writes the data to the session and gets the data by Session::get method.
Returns a redirect to a named route

return Redirect::route (' login ');
Returns a named route that is redirected to a parameter

return Redirect::route (' profile ', array (1));
Returns a named route that is redirected to a named parameter

return Redirect::route (' profile ', array (' user ' => 1));
Returns a redirect to Controller action

Return redirect::action (' Homecontroller@index ');
Returns a redirect to the controller action with parameters

Return redirect::action (' Usercontroller@profile ', Array (1));
Returns a redirect to the controller action with a named parameter

Return redirect::action (' Usercontroller@profile ', array (' user ' => 1));

View

The view usually contains HTML code in the application, which facilitates separating the presentation layer from the controller and business logic. Views are stored in the App/views directory.

A simple view pattern example:

Copy Code code as follows:

<!--View stored in app/views/greeting.php-->

<body>
</body>

Return the view to the browser by using the following methods:

Copy Code code as follows:

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

The second argument passed to the View::make method is an array that is passed to the view.

Passing data to a view

Copy Code code 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 ');

Passing a child view to a view

Perhaps you might want to put a view into another view. For example, the child view stored in the app/views/child/view.php file is passed to another view, as follows:

Copy Code code as follows:

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

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

The child view can be exported in the parent view:

Copy Code code as follows:

<body>
<?php echo $child;?>
</body>

View synthesizer

The view synthesizer can be either a callback function or a class method, which is invoked when the view is created. Use a view synthesizer to organize your code into one place, if you want to bind some data to it each time you create a view in your application. Therefore, the view synthesizer is like the view model or the moderator.

Define a View synthesizer

Copy Code code as follows:

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

Count will now be bound to the view each time you create a profile view.

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

Copy Code code as follows:

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

If you prefer to use a class-based view synthesizer, IoC container can provide more convenience, as follows:

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

The view synthesizer class is defined as follows:

Copy Code code as follows:

Class Profilecomposer {

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

}

Note that there is no provision for where the view synthesizer classes are stored. Therefore, you can store it arbitrarily, as long as you can specify the location in the Composer.json file and automatically load it.

View Builder

The View builder works almost exactly the same as the view synthesizer, except that the view creator is immediately triggered when a view is instantiated. The view builder is easily defined by the Creator method:

Copy Code code 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 a file download response

Return Response::d ownload ($pathToFile);

Return Response::d ownload ($pathToFile, $status, $headers);
Note: Symfony httpfoundation is used to process file downloads and requires that the file name of the downloaded files contain only ASCII characters.

Response macros

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

Copy Code code as follows:

Response::macro (' Caps ', function ($value)
{
Return Response::make (Strtoupper ($value));
});

The macro method accepts two parameters, a specified and a name, and a closure. When a macro with that name is invoked through the Response class, the closure is executed:

return response::caps (' foo ');
You can define macros in the files in the App/start directory. Alternatively, you can organize your macros through a separate file and include the file in 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.