Drest\Service::setupRequest PHP Method

setupRequest() protected method

Called on successful routing of a service call Prepares the service to a request to be rendered
protected setupRequest ( ) : boolean
return boolean $result - if false then fail fast no call to runCallMethod() should be made.
    protected function setupRequest()
    {
        // Make sure we have a route matched (this should caught and an exception thrown on Manager::determineRoute())
        if (!$this->matched_route instanceof RouteMetaData) {
            return false;
        }
        // Proceed to run the service action
        if ($this->matched_route->isExposeDisabled()) {
            return true;
        }
        // If its a GET request and no expose fields are present, fail early
        if ($this->getRequest()->getHttpMethod() == Request::METHOD_GET) {
            $expose = $this->matched_route->getExpose();
            if (count($expose) === 0 || count($expose) == 1 && empty($expose[0])) {
                $this->renderDeterminedRepresentation($this->getActionInstance()->createResultSet(array()));
                return false;
            }
        }
        return true;
    }

Usage Example

示例#1
0
 /**
  * Execute a dispatched request
  * @param  string                            $namedRoute  - Define the named Route to be dispatched - bypasses the internal router lookup
  * @param  array                             $routeParams - Route parameters to be used for dispatching a namedRoute request
  * @throws Route\NoMatchException|\Exception
  */
 protected function execute($namedRoute = null, array $routeParams = array())
 {
     if (($route = $this->determineRoute($namedRoute, $routeParams)) instanceof RouteMetaData) {
         // Get the representation to be used - always successful or it throws an exception
         $representation = $this->getDeterminedRepresentation($route);
         // Configure push / pull exposure fields
         switch ($this->request->getHttpMethod()) {
             // Match on content option
             case Request::METHOD_GET:
                 $this->handlePullExposureConfiguration($route);
                 break;
                 // Match on content-type
             // Match on content-type
             case Request::METHOD_POST:
             case Request::METHOD_PUT:
             case Request::METHOD_PATCH:
                 $representation = $this->handlePushExposureConfiguration($route, $representation);
                 break;
         }
         // Set the matched service object and the error handler into the service class
         $this->service->setMatchedRoute($route);
         $this->service->setRepresentation($representation);
         $this->service->setErrorHandler($this->getErrorHandler());
         // Set up the service for a new request
         if ($this->service->setupRequest()) {
             $this->service->runCallMethod();
         }
     }
 }