Skip to content

Instantly share code, notes, and snippets.

@patrickmaciel
Created March 17, 2015 19:23
Show Gist options
  • Select an option

  • Save patrickmaciel/e535a245837b89316baa to your computer and use it in GitHub Desktop.

Select an option

Save patrickmaciel/e535a245837b89316baa to your computer and use it in GitHub Desktop.
Laravel 4.2 ResponseBuilder - manipulando o render e redirect baseado no tipo de requisição
<?php
use Illuminate\Routing\Controller;
use Libraries\Wrappers\http\ResponseBuilder;
abstract class BaseController extends Controller
{
/**
* The response builder that is going to be used in order to prepare
* action responses
*
* @var \Pulse\Base\responseBuilder
*/
protected $responseBuilder;
/**
* Sets the responseBuilder attribute of the controller
*/
public function __construct(ResponseBuilder $responseBuilder)
{
$this->responseBuilder = $responseBuilder;
// Clockwork package
// $this->beforeFilter(function()
// {
// Event::fire('clockwork.controller.start');
// });
// $this->afterFilter(function()
// {
// Event::fire('clockwork.controller.end');
// });
}
/**
* Setup the layout used by the controller.
*
* @return void
*/
protected function setupLayout()
{
if (!is_null($this->layout)) {
$this->layout = View::make($this->layout);
}
}
/**
* Make a view using the attached responseBuilder object
*
* @param string $view
* @param array $data
* @param array $viewData Data that will be passed to the View only, so it will not be visible in Json responses
*
* @return Illuminate\Support\Contracts\RenderableInterface a renderable View or Response object
*/
protected function render($view, $data = array(), $viewData = array())
{
if (empty($data['error'])) {
$data['error'] = false;
}
return $this->responseBuilder->render($view, $data, $viewData);
}
/**
* Builds a redirect response to be used when the operation has been performed
* successfully. Also if the client accepts json, a proper json response will
* be returned.
*
* @param string $route
* @param array $data
* @param int $status
* @param array $headers
* @return \Illuminate\Http\RedirectResponse
*/
public function redirectSuccess($route, $data = array(), $status = 302, $headers = array())
{
return $this->responseBuilder->redirectSuccess($route, $data, $status, $headers);
}
/**
* Builds a redirect response to be used when the operation has failed. Also
* if the client accepts json, a json containing the error message will be
* returned.
*
* @param string $route
* @param array $data
* @param int $status
* @param array $headers
* @return \Illuminate\Http\RedirectResponse
*/
public function redirectFailed($route, $data = array(), $status = 302, $headers = array())
{
return $this->responseBuilder->redirectFailed($route, $data, $status, $headers);
}
}
<?php
namespace Libraries\Wrappers\http;
use App;
use Illuminate\Support\Contracts\ArrayableInterface;
use Input;
use Request;
use Session;
class ResponseBuilder
{
/**
* Builds a response using the view and the given data. It will primarily
* render the $view file with the $data, but if the request asks for Json
* A Json response with the same $data will be returned.
*
* @param string $view
* @param array $data
* @param array $viewData Data that will be passed to the View only, so it will not be visible in Json responses
*
* @return Illuminate\Support\Contracts\RenderableInterface a renderable View or Response object
*/
public function render($view, $data = array(), $viewData = array())
{
if (Request::isJson() || Request::wantsJson() || Input::get('json', false)) {
$response = App::make('Response')->json($this->morphToArray($data));
} else {
$response = App::make('Response')->view($view, array_merge($data, $viewData));
}
return $response;
}
/**
* Morph the given content into Array.
*
* @param mixed $content
*
* @return string
*/
protected function morphToArray($content)
{
if ($content instanceof ArrayableInterface || method_exists($content, 'toArray')) {
return $content->toArray();
}
if (is_array($content)) {
foreach ($content as $key => $value) {
$content[$key] = $this->morphToArray($value);
}
}
return $content;
}
/**
* Builds a redirect response to be used when the operation has been performed
* successfully. Also if the client accepts json, a proper json response will
* be returned.
*
* @param string $route
* @param array $data
* @param int $status
* @param array $headers
* @return \Illuminate\Http\RedirectResponse
*/
public function redirectSuccess($route, $data = array(), $status = 302, $headers = array())
{
if (Request::isJson() || Request::wantsJson() || Input::get('json', false)) {
$json = ['status' => 'success'];
if (
Session::has('success') &&
((strpos($route, 'back') === false) && (strpos($route, 'index') === false))
) {
$data['message'] = Session::get('success');
}
$response = App::make('Response')->json($json);
} else {
if (isset($data['_old_input'])) {
Session::flash('_old_input', $data['_old_input']);
unset($data['_old_input']);
}
if ($route == 'back') {
$response = App::make('redirect')->back($status, $headers);
} else {
$response = App::make('redirect')->route($route, $data, $status, $headers);
}
}
return $response;
}
/**
* Builds a redirect response to be used when the operation has failed. Also
* if the client accepts json, a json containing the error message will be
* returned.
*
* @param string $route
* @param array $data
* @param int $status
* @param array $headers
* @return \Illuminate\Http\RedirectResponse
*/
public function redirectFailed($route, $data = array(), $status = 302, $headers = array())
{
if (Request::isJson() || Request::wantsJson() || Input::get('json', false)) {
$content = [
'status' => 'fail',
'errors' => $this->morphToArray(array_get($data, 'errors')),
];
if (
Session::has('error') &&
((strpos($route, 'back') === false) && (strpos($route, 'index') === false))
) {
$data['message'] = Session::get('error');
}
if (isset($data['_old_input'])) {
$content['_old_input'] = $data['_old_input'];
}
$response = App::make('Response')
->json($content);
} else {
if (isset($data['_old_input'])) {
Session::flash('_old_input', $data['_old_input']);
unset($data['_old_input']);
}
if (isset($data['errors'])) {
Session::flash('errors', $data['errors']);
unset($data['errors']);
}
if ($route == 'back') {
$response = App::make('redirect')->back($status, $headers);
} else {
$response = App::make('redirect')
->route($route, $data, $status, $headers);
}
}
return $response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment