Laravel Custom Responses
Описание на русском, (*1)
Install
Require this package with composer:, (*2)
composer require "vanchelo/laravel-custom-responses dev-master"
After updating composer, add the ServiceProvider to the providers array in app/config/app.php, (*3)
'Vanchelo\CustomResponses\ResponsesServiceProvider'
Create responses folder in app/views and three blade templates:
defult.blade.php, 403.blade.php, 404.blade.php, (*4)
How to use
In controller:, (*5)
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., (*6)
- 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 on app/views/responses, (*7)
-
Put this code in app/start/gobal.php:, (*8)
App::bind('401', 'Acme\Responses\Unauthorized');
- That's all