Laravel Responses and views

Source: Internet
Author: User
Base response

Direct response string
Route::get (' example/test20 ', function () {
Return ' Hello World ';
});
Customizing HTTP Responses
Route::get (' example/test21 ', function () {
return Response::make (' content not present ', 404);
});
Response View
Route::get (' example/test22 ', function () {
Return Response::view (' test22 ');
});
Add a cookie to a 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 ');
Redirect values named routes with named arguments
return Redirect::route (' profile ', array (' user ' = 1));
Redirect to the specified controller method
Return redirect::action (' Homecontroller@index ');
Redirect to the specified controller method and can take the parameter
Return redirect::action (' Usercontroller@profile ', array (' user ' = 1));

Response View

Responding to views and passing parameters
Route::get (' Example/test30 ', function () {
The first way
Return View::make (' Test30 ', Array (' name ' = ' xiaoming '));
The second way
Return View::make (' Test30 ')->with (' name ', ' xiaoming2 ');
The Third Way
Return View::make (' Test30 ')->withname (' Xiaoming3 ');
Fourth Way, note: share the same data in all views
View::share (' name ', ' Steve ');
});
Passing in a child view in a view
Route::get (' Example/test31 ', function () {
$data = Array (' name ' = ' John ');
The child view is placed in the app/views/child/child_view.php, and you can pass the variable to it
Return View::make (' Test30 ')->with ($data)->nest (' Child ', ' Child.child_view ', $data);
});

View component or view synthesizer

If you want the view to be created, you can define the view component by tying the specified data:

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

To add multiple views to a view component:

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

If you are using class-based view components:

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

The view component class is created like this:

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

Special response

Creating a JSON response
return Response::json (Array (' name ' = ' Steve ', ' state ' = ' CA '));
Create JSONP response
return Response::json (Array (' name ' = ' Steve ', ' state ' = ' CA ') ')->setcallback (Input::get (' callback '));
File Download response
Return Response::d ownload ($pathToFile);
Return Response::d ownload ($pathToFile, $name, $headers);
Note: The class library Symfony httpfoundation that manages file downloads requires the file name to be ASCII encoded.

Response macros, using Response::macro to customize responses

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

The macro method accepts two parameters, one specifying the macro name and one closure. When a macro of that name is called 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 macro with a separate file and include the file in a start file.

Reproduced in 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.