#Laravel Custom Responses
##Install
Require this package with composer:
composer require "vanchelo/laravel-custom-responses dev-master"
After updating composer, add the ServiceProvider to the providers array in app/config/app.php
'Vanchelo\CustomResponses\ResponsesServiceProvider'
Create responses
folder in app/views
and three blade templates:
defult.blade.php
, 403.blade.php
, 404.blade.php
##How to use
In controller:
class PageController extends Controller
{
public function index($id)
{
if ( ! $page = Page::find($id)) App::abort(404);
// or
if ( ! $page = Page::find($id)) return App::make(404);
return View::make('page', compact('page'));
}
}
##Create you own custom response
For example we will create custom response for 401 (Unauthorized) status code.
- Create class and put it on your app folder
<?php namespace Acme\Responses;
// app/Acme/Responses/Unauthorized.php
class Unauthorized extends Response
{
protected $view = 'responses.401';
protected $defaultCode = 401;
}
-
Create blade template
401.blade.php
and put it onapp/views/responses
-
Put this code in
app/start/gobal.php
:
App::bind('401', 'Acme\Responses\Unauthorized');
- That's all