Responses in Laravel

By Venkata Krishna In Artisan CLI, Cache, Routing, Views, Laravel Tips - - Comments(0) - Views(6618)
Tags : laravel, laravel views, laravel blade templating, laravel redirects, laravel custom responses

In our previous tutorials we saw how we can route URI's using Closures and Controllers actions when we got a Request using web server. But at the same we are also sending a response  to every request. But today we will discuss in detail about the responses  in Laravel.

Let's go to our first web request http://domain.com/blog/ and see what kind of response we are sending via route closure below.

// app/routes.php

Route::get('/', function()
{

   return 'Blog Home Page!';

});

Here in the above example we are returning a statement called 'Blog Home Page!' to the browser when we get request using a route closure or controller action always.

Let's see how we can return HTML as a response using route Closure.

// app/routes.php

Route::get('/', function()
{

   return '<!doctype html><html lang="en"><head><mets chartset="UTF-8"><title>Blog- Home Page</title></head><<body><p>This is blog home page</p></body></html>';

});

Now it's not a good option to send HTML as a response in this way when Laravel is a MVC framework and we can send a View response.

Views

Views are basically visual part of the application. In a Model View Controller design pattern they make up the View portion. That's why they are called views. A view is just a plain text template that can be returned to the browser, though it's likely that your views will contain HTML. Views use .blade.php extension and are located within /app/views directory.  Blade is a simple, yet powerful templating engine provided with Laravel. Unlike controller layouts, Blade is driven by  template inheritance and sections. All Blade templates should use the .blade.php extension. We will learn about blade templating in detail in up coming lessons .

Let's create a simple view.

//  app/views/index.blade.php

 <!doctype html>
   <html lang="en">
     <head>
       <meta charset="UTF-8">
       <title>Blog!</title>
     </head>
     <body>
       <p>It's Home Page!</p>
     </body>
   </html>

Now let's return this newly created view using a route closure.

// app/routes.php

Route::get('/', function()
{

   return View::make('index');

});

In the above example the View::make() method was used to create a new instance of View response object. The first parameter to the make() method is the template name which we created in app/views directory.

Now in the above example we are just returning a simple static html template as a view response. But now let's see how we can send data to the view template from route closure.

// app/routes.php

Route::get('/{blogId}', function($blogId)
{
   $data['blogId'] = $blogId;

   return View::make('page',$data);

});

In the above example route we take $blogId parameter and add it to our view data array. If you observe the second parameter to the make() method accepts an array that is passed to the view template. In the resulting view template we are able to access the value like this.

//  app/views/page.php

 <!doctype html>
   <html lang="en">
     <head>
       <meta charset="UTF-8">
       <title>Blog</title>
     </head>
     <body>
       <p>It's {{$blogId}} Blog Page!</p>
     </body>
   </html>

Now we request in any web browser http://domain.com/blog/134, then we can see response in the browser screen as It's 134 Blog Page!. It's simple to return HTML templates as views with dynamic data instead of some random strings.

In some cases we may need to redirect the flow of our application to another route or logic portion. In those cases we need to use Redirect response object.

Redirects

Redirect  is a special type of response object which redirects the flow of application to another route. Let's see how we can this with a small example like we wanted to redirect the users to a coming soon page when they request the home page.

// app/routes.php

Route::get('/', function()
{
   return Redirect::to('comingsoon');
});

Route::get('comingsoon', function(
{
   return View::make('coming_soon');
});

Custom Responses

Both View and Redirect inherit from the Laravel response object. The response object contains a body, a status code, HTTP headers and other useful information just like how a http response object contains. So we can create our own response object to suit our own needs. Let's see how we can make custom response.

// app/routes.php

Route::get('custom/response', function()
{

  return Response::make('Hello World!', 200);

});

In the above example we use the Response::make() method to create a new response object. The first parameter is the content or body of the response and the second parameter is the HTTP  status code that will be served with the response. There are lot of HTTP status codes like 302, 404 etc. Each code has specific meaning and browser will show the response based on the status code which we are sending. HTTP headers are collection of key value pairs of data which represent useful information to the web browser that is receiving the response. Let's see how can we set our own HTTP headers values.

// app/routes.php

Route::get('custom/response', function()
{

  $response = Response::make('Hello World!', 200);
  
  $response->headers->set('our key', 'our value');

  return $response;

});

Let's see other sets of responses like JSON and Download. In certain we may have to send a JSON response or we may need to trigger a file download. In those cases our response will be like below.

// app/routes.php

Route::get('ajax/response', function()
{

  $data= array('iron','man', 'rocks');

  return Response::json($data);

});
Route::get('file/download', function()
{

  $file= 'path_to_my_file.pdf';

  return Response::download($file);

});

 

These are the ways we can send response for various types of requests using Laravel framework. Thanks for reading the tutorial.

Thanks

KodeInfo

Author

Irfan is a web developer and consultant from India. He is the co-founder of KodeInfo, the PHP and Laravel Community . In the meantime he follows other projects, manages everything related to marketing and seo for kodeinfo , works as a freelance backend consultant for PHP applications and studies IT Engineering . He loves to learn new things, not only about PHP or development but everything.

comments powered by Disqus