dev-master
9999999-dev
The Requires
- php >=5.4.0
- willdurand/negotiation @stable
The Development Requires
Wallogit.com
2017 © Pedro Peláez
This library provides helpful content negotiations. For instance if you ask for json back from a typical laravel website you'd still get html back. I carefully look at the Accept header and type (.json for instance) to best determine what media type to render., (*2)
What does this mean? It means your controller actions are cleaner, all you do is return values! ~~~ php class UserController extends BaseController { public function show() { return View::make('user.show')->with([ 'name' => 'BlaineSch', ]); } }, (*3)
Now, let's respond to multiple content types and prettify our controller! ~~~ php class UserController extends BaseController { public function showAction() { return ['name' => 'blainesch']; } }
~~~ json 'require': { "blainesch/laravel-pretty-controller": "0.0.1" }, (*4)
### Update `Controller` and add `CoreController` values in your `app/config/app.php` file. ~~~ php 'aliases' => [ // ... 'Controller' => 'Blainesch\LaravelPrettyController\Action\PrettyController', 'CoreController' => 'Illuminate\Routing\Controller', // ... ]
Create a bootstrap/media.php
~~~ php
<?php, (*5)
use Blainesch\LaravelPrettyController\Http\MediaType;, (*6)
MediaType::add('html', [ 'conditions' => [ 'accept' => [ 'text/html', '/', ], ], 'encode' => function($request, $response) { $class = strtolower(str_replace('Controller', '', $request['controller'])); return \View::make("{$class}.{$request['method']}")->with($response); }, ]);, (*7)
MediaType::add('json', [ 'conditions' => [ 'type' => 'json', 'accept' => [ 'application/json', 'application/x-json', ], ], 'encode' => function($request, $response) { return json_encode($response); }, ]);, (*8)
Include this file in `bootstrap/autoload.php` below composer autoloader ~~~ php require __DIR__.'/media.php';