Skip to content

Instantly share code, notes, and snippets.

@richthegeek
Created March 12, 2012 21:37
Show Gist options
  • Save richthegeek/2024836 to your computer and use it in GitHub Desktop.
Save richthegeek/2024836 to your computer and use it in GitHub Desktop.
Laravel: response mutators
<?php
Route::filter('after', function($response)
{
$format = Input::get('format', Config::get('application.default_mutator'));
$response->mutator = $format;
});
Route::mutator('string', function($response) {
$response->content = (string) $response->content;
return $response;
});
Route::mutator('json', function($response) {
if ($json = @json_encode($response->content)) {
$response->content = $json;
return $response;
} else {
return $response->mutate('string');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment