Example #1
0
 /**
  * Call internal URI with parameters.
  *
  * @param  string $uri
  * @param  string $method
  * @param  array  $parameters
  * @return mixed
  */
 public function invoke($uri, $method, $parameters = array())
 {
     // Request URI.
     $uri = '/' . ltrim($uri, '/');
     // Parameters for GET, POST
     $parameters = $parameters ? current($parameters) : array();
     try {
         // store the original request data and route
         $originalInput = $this->request->input();
         $originalRoute = $this->router->getCurrentRoute();
         // create a new request to the API resource
         $request = $this->request->create($uri, strtoupper($method), $parameters);
         // replace the request input...
         $this->request->replace($request->input());
         $dispatch = $this->router->dispatch($request);
         if (method_exists($dispatch, 'getOriginalContent')) {
             $response = $dispatch->getOriginalContent();
         } else {
             $response = $dispatch->getContent();
         }
         // Decode json content.
         if ($dispatch->headers->get('content-type') == 'application/json') {
             if (function_exists('json_decode') and is_string($response)) {
                 $response = json_decode($response, true);
             }
         }
         // replace the request input and route back to the original state
         $this->request->replace($originalInput);
         $this->router->setCurrentRoute($originalRoute);
         return $response;
     } catch (NotFoundHttpException $e) {
     }
 }