/** * Execute an action on the controller. * Overridden to perform output transformations. * * @param string $method * @param array $parameters * @return \Symfony\Component\HttpFoundation\Response */ public function callAction($method, $parameters) { $response = $this->response(); $action_response = parent::callAction($method, $parameters); switch (true) { case $action_response instanceof Model: $response->setContent(Api::transform($action_response)); break; case $action_response instanceof Collection: $output = array(); foreach ($action_response as $model) { $output[] = Api::transform($model); } $response->setContent($output); break; case $action_response instanceof Paginator: $output = array(); foreach ($action_response->getCollection() as $model) { $output[] = Api::transform($model); } $response->setContent($output)->header('Pagination-Page', $action_response->currentPage())->header('Pagination-Num', $action_response->perPage())->header('Pagination-Total', $action_response->total())->header('Pagination-Last-Page', $action_response->lastPage()); break; case $action_response instanceof Response: $response = $action_response; break; default: $response->setContent($action_response); } return $response; }
/** * Add route filters. * * @return void */ protected function bootFilters() { if (config('api.cors_enabled', true)) { $this->app['router']->before(function ($request) { if (Request::header('Origin') && $_SERVER['REQUEST_METHOD'] === 'OPTIONS') { $response = Response::make(null, 204); Cors::attachHeaders($response); Cors::attachOriginHeader($response, Request::header('Origin')); return $response; } }); $this->app['router']->after(function ($request, $response) { if (Request::header('Origin')) { Cors::attachHeaders($response); Cors::attachOriginHeader($response, Request::header('Origin')); } }); } $this->app['router']->filter('protect', function ($route, $request) { Api::protect(); }); $this->app['router']->filter('checkscope', function ($route, $request, $scope = '') { // B/c Laravel uses : as a special character already. $scope = str_replace('.', ':', $scope); Api::checkScope($scope); }); }