public function onDispatch(MvcEvent $e) { $this->range = null; $this->model = $this->acceptableViewModelSelector($this->options->getAcceptCriteria()); $this->options->getDocumentManager()->getEventManager()->addEventSubscriber($this); return parent::onDispatch($e); }
/** * Handle the request * * @param MvcEvent $e * @return mixed */ public function onDispatch(MvcEvent $e) { try { parent::onDispatch($e); } catch (\Exception $e) { return $this->getResponse()->setStatusCode(500)->setContent($e->__toString()); } }
public function onDispatch(MvcEvent $e) { $sm = $e->getApplication()->getServiceManager(); $this->config = $sm->get('Config'); $this->session = new Container($this->config['session']['container_name']); // Pass it up to the parent parent::onDispatch($e); }
/** * ZF требует, чтобы клиент передавал id с именем $identifierName в GET-запросе либо чтобы * в маршруте распознавался id также с именем $identifierName. * Маршрут для каждого запроса (с разным именем id) писать накладно. * Переделывать чтобы клиент передавал в GET-запросе имя id не хочется. * Поэтому тут сделано подстановка в запрос id с именем $identifierName. */ public function onDispatch(MvcEvent $e) { if ($this->getIdentifierName() != "id") { $routeMatch = $e->getRouteMatch(); $id = $routeMatch->getParam("id", false); $routeMatch->setParam($this->getIdentifierName(), $id); } return parent::onDispatch($e); }
public function onDispatch(MvcEvent $e) { $response = parent::onDispatch($e); $request = $e->getRequest(); $method = strtolower($request->getMethod()); if ($method === "options") { $e->setResult($this->options()); return $e; } return $response; }
/** * Handle dispatch event * * Does several "pre-flight" checks: * - Raises an exception if no resource is composed. * - Raises an exception if no route is composed. * - Returns a 405 response if the current HTTP request method is not in * $options * * @param MvcEvent $e * @return mixed|ProblemResponse * @throws \Parrot\API\Resource\Exception\ResourceNotImplementedException */ public function onDispatch(MvcEvent $e) { if (!$this->getResource() && !$this->locateResource($e->getRouteMatch()->getParam('resource'))) { throw new ResourceNotImplementedException(); } // Check for an API-Problem in the event $return = $e->getParam('api-problem', false); // If no API-Problem, dispatch the parent event if (!$return) { $return = parent::onDispatch($e); } if (!$return instanceof Problem) { return $return; } if ($return instanceof Problem) { return new ProblemResponse($return); } }
/** * Override this function to ensure that all values returned is always a JsonModel * @param MvcEvent $e * @return JsonModel * */ public function onDispatch(MvcEvent $e) { try { $return = parent::onDispatch($e); } catch (\Exception $ex) { $previous = $ex->getPrevious(); if ($previous) { $ex = $previous; } return $this->responseError(500, $ex->getMessage()); } if (is_array($return) || $return instanceof \Iterator || $return instanceof \ArrayObject) { // encapsulate arrays, Iterator, or ArrayObject in a JsonModel $return = new JsonModel($return); $e->setResult($return); } else { if (!$return instanceof JsonModel && !$return instanceof Response) { // unrecognized return type, return output as is return $this->responseError(500, 'Unknown return format'); } } return $return; }
public function getList() { parent::notFoundAction(); }
/** * @param null|int $id * @return mixed */ public function head($id = null) { return parent::head($id); // @TODO: Change the autogenerated stub }
public function getList() { if (!$this->getOptions()->getRestEnabled()) { return parent::getList(); } $this->getResponse()->getHeaders()->addHeaderLine('Content-Type', 'application/json'); $result = array(); $items = $this->getAll(); foreach ($items as $item) { $result[] = $this->itemToSerializable($item); } return new JsonModel($result); }
public function setEventManager(EventManagerInterface $events) { parent::setEventManager($events); $this->events->attach('dispatch', [$this, 'checkOptions'], 10); }
/** * Handle the dispatch event * * Does several "pre-flight" checks: * - Raises an exception if no resource is composed. * - Raises an exception if no route is composed. * - Returns a 405 response if the current HTTP request method is not in * $options * * When the dispatch is complete, it will check to see if an array was * returned; if so, it will cast it to a view model using the * AcceptableViewModelSelector plugin, and the $acceptCriteria property. * * @param MvcEvent $e * @return mixed * @throws Exception\DomainException */ public function onDispatch(MvcEvent $e) { if (!$this->resource) { throw new Exception\DomainException(sprintf('%s requires that a %s\\ResourceInterface object is composed; none provided', __CLASS__, __NAMESPACE__)); } if (!$this->route) { throw new Exception\DomainException(sprintf('%s requires that a route name for the resource is composed; none provided', __CLASS__)); } // Check for an API-Problem in the event $return = $e->getParam('api-problem', false); // If no API-Problem, dispatch the parent event if (!$return) { $return = parent::onDispatch($e); } if (!$return instanceof ApiProblem && !$return instanceof HalResource && !$return instanceof HalCollection) { return $return; } $viewModel = $this->acceptableViewModelSelector($this->acceptCriteria); $viewModel->setVariables(array('payload' => $return)); if ($viewModel instanceof View\RestfulJsonModel) { $viewModel->setTerminal(true); } $e->setResult($viewModel); return $viewModel; }
public function setEventManager(EventManagerInterface $events) { // events property defined in AbstractController $this->events = $events; parent::setEventManager($events); // Register the listener and callback method with a priority of 10 $events->attach('dispatch', array($this, 'checkOptions'), 10); }
/** * @return Request */ public function getRequest() { return parent::getRequest(); }
public function onDispatch(MvcEvent $e) { $this->model = $this->acceptableViewModelSelector($this->options->getAcceptCriteria()); return parent::onDispatch($e); }
/** * Handle the dispatch event * * Does several "pre-flight" checks: * - Raises an exception if no resource is composed. * - Raises an exception if no route is composed. * - Returns a 405 response if the current HTTP request method is not in * $options * * When the dispatch is complete, it will check to see if an array was * returned; if so, it will cast it to a view model using the * AcceptableViewModelSelector plugin, and the $acceptCriteria property. * * @param MvcEvent $e * @return mixed * @throws DomainException */ public function onDispatch(MvcEvent $e) { if (! $this->getResource()) { throw new DomainException(sprintf( '%s requires that a %s\ResourceInterface object is composed; none provided', __CLASS__, __NAMESPACE__ )); } if (! $this->route) { throw new DomainException(sprintf( '%s requires that a route name for the resource is composed; none provided', __CLASS__ )); } // Check for an API-Problem in the event $return = $e->getParam('api-problem', false); // If no return value dispatch the parent event if (! $return) { $return = parent::onDispatch($e); } if (! $return instanceof ApiProblem && ! $return instanceof HalEntity && ! $return instanceof HalCollection ) { return $return; } if ($return instanceof ApiProblem) { return new ApiProblemResponse($return); } // Set the fallback content negotiation to use HalJson. $e->setParam('ZFContentNegotiationFallback', 'HalJson'); // Use content negotiation for creating the view model $viewModel = new ContentNegotiationViewModel(array('payload' => $return)); $e->setResult($viewModel); return $viewModel; }
protected function attachDefaultListeners() { parent::attachDefaultListeners(); $events = $this->getEventManager(); $events->attach('dispatch', array($this, 'preDispatch'), 100); }
/** * Modify an entire resource collection * * @param array|\Traversable $items * @return array|\Traversable */ public function patchList($items) { //This isn't great code to have in a test class, but I seems the simplest without BC breaks. if (isset($items['name']) && $items['name'] == 'testDispatchViaPatchWithoutIdentifierReturns405ResponseIfPatchListThrowsException') { parent::patchList($items); } return $items; }
public function onDispatch(MvcEvent $event) { $this->model = $this->acceptableViewModelSelector($this->acceptCriteria); return parent::onDispatch($event); }
/** * @override * dispatch * * @param RequestInterface $request * @param ResponseInterface $response * * @return mixed|ResponseInterface */ public function dispatch(RequestInterface $request, ResponseInterface $response = null) { $this->request = $request; $response = $this->getResponse(); return parent::dispatch($request, $response); }
/** * Validate the API request and set global options. * * @param MvcEvent $event */ public function onDispatch(MvcEvent $event) { $request = $this->getRequest(); // Set pretty print. $prettyPrint = $request->getQuery('pretty_print'); if (null !== $prettyPrint) { $this->setViewOption('pretty_print', true); } // Set the JSONP callback. $callback = $request->getQuery('callback'); if (null !== $callback) { $this->setViewOption('callback', $callback); } try { // Finish dispatching the request. $this->checkContentType($request); parent::onDispatch($event); } catch (\Exception $e) { $this->getServiceLocator()->get('Omeka\\Logger')->err((string) $e); return $this->getErrorResult($event, $e); } }
/** * Set the event manager instance used by this context * * @param EventManagerInterface $events * * @return $this|\Zend\Mvc\Controller\AbstractController */ public function setEventManager(EventManagerInterface $events) { parent::setEventManager($events); $this->events = $events; // Register a listener at high priority $events->attach('dispatch', function (MvcEvent $e) { if (!isset($this->entityClass)) { throw new \Exception('Controller is not properly set up. "entityClass" is missing', 500); } //Check is method is NOT allowed if (!in_array($e->getRequest()->getMethod(), $this->allowedOptions)) { /** @var Response $response */ $response = $e->getResponse(); // Return 405 $response->setStatusCode(405); //Indicate what _is_ allowed $response->getHeaders()->addHeaderLine('Allow', implode(',', $this->allowedOptions)); return $response; } return true; }, 10); return $this; }
public function onDispatch(MvcEvent $e) { //$this->config($e); $this->controller = $e->getTarget(); parent::onDispatch($e); }
public function onDispatch(MvcEvent $event) { $collection = $this->params()->fromRoute('collection'); if (!$this->settingsManager->has($collection)) { return new ApiProblemResponse(new ApiProblem(404, 'Settings collection not found.')); } $this->settings = $this->settingsManager->get($collection); return parent::onDispatch($event); }