/** * @param ServerRequestInterface $request * @param ResponseInterface $response * * @return HtmlResponse|RedirectResponse */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response) { if ($request->getMethod() === 'POST') { $this->albumService->addAlbum($request->getParsedBody()); return new RedirectResponse($this->router->generateUri('album.index')); } return new HtmlResponse($this->template->render('album::add', ['form' => $this->form])); }
/** * @param array $params * @return string * @throws Exception\RenderingException if current result is a routing * failure. */ private function generateUriFromResult(array $params) { if ($this->result->isFailure()) { throw new Exception\RenderingException('Attempting to use matched result when routing failed; aborting'); } $name = $this->result->getMatchedRouteName(); $params = array_merge($this->result->getMatchedParams(), $params); return $this->router->generateUri($name, $params); }
/** * @param ServerRequestInterface $request * @param ResponseInterface $response * * @return HtmlResponse|RedirectResponse */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response) { try { $album = (array) $this->albumService->getAlbum($request->getAttribute('id')); } catch (\Exception $e) { return new HtmlResponse($this->template->render('error::404'), 404); } if ($request->getMethod() === 'POST') { $body = new Parameters($request->getParsedBody()); $del = $body->get('del', 'No'); if (strtolower($del) === 'yes') { $this->albumService->deleteAlbum($album); } return new RedirectResponse($this->router->generateUri('album.index')); } return new HtmlResponse($this->template->render('album::delete', ['album' => $album])); }
/** * Assembles a route with given name using given information * * @param string|null $name * @param array|bool $routeParams * @param array|bool $queryParams * @param bool $inherit Tells if route and query params should be inherited from current route * @return string */ public function assembleUrl($name = null, $routeParams = [], $queryParams = [], $inherit = false) { $routeResult = $this->getCurrentRouteResult(); $name = $name ?: $routeResult->getMatchedRouteName(); if (is_bool($routeParams)) { $inherit = $routeParams; $routeParams = []; } if (is_bool($queryParams)) { $inherit = $queryParams; $queryParams = []; } if ($inherit) { $routeParams = array_merge($routeResult->getMatchedParams(), $routeParams); $queryParams = array_merge($this->request->getQueryParams(), $queryParams); } $queryString = empty($queryParams) ? '' : sprintf('?%s', http_build_query($queryParams)); return $this->router->generateUri($name, $routeParams) . $queryString; }
/** * Process an incoming request and/or response. * * Accepts a server-side request and a response instance, and does * something with them. * * If the response is not complete and/or further processing would not * interfere with the work done in the middleware, or if the middleware * wants to delegate to another process, it can use the `$out` callable * if present. * * If the middleware does not return a value, execution of the current * request is considered complete, and the response instance provided will * be considered the response to return. * * Alternately, the middleware may return a response instance. * * Often, middleware will `return $out();`, with the assumption that a * later middleware will return a response. * * @param Request $request * @param Response $response * @param null|callable $out * @return null|Response */ public function __invoke(Request $request, Response $response, callable $out = null) { // Make sure the short URL exists for this short code $shortCode = $request->getAttribute('shortCode'); try { $shortUrl = $this->urlShortener->shortCodeToUrl($shortCode); if (!isset($shortUrl)) { return $out($request, $response->withStatus(404), 'Not Found'); } } catch (InvalidShortCodeException $e) { $this->logger->warning('Tried to create a QR code with an invalid short code' . PHP_EOL . $e); return $out($request, $response->withStatus(404), 'Not Found'); } $path = $this->router->generateUri('long-url-redirect', ['shortCode' => $shortCode]); $size = $this->getSizeParam($request); $qrCode = new QrCode($request->getUri()->withPath($path)->withQuery('')); $qrCode->setSize($size)->setPadding(0); return new QrCodeResponse($qrCode); }
/** * @param ServerRequestInterface $request * @param ResponseInterface $response * @return ResponseInterface */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response) { try { /** * @var Session $session */ $session = $request->getAttribute('session'); $album = new Album(); $this->form->bind($album); $this->form->get('submit')->setValue('Add'); if ($request->getMethod() === 'POST') { $this->albumService->addAlbum($request->getParsedBody()); $session->getSegment('App\\Album')->setFlash('flash', ['type' => 'success', 'message' => sprintf('Successfully added album %s (%s)', $album->getTitle(), $album->getArtist())]); return new RedirectResponse($this->router->generateUri('album.index')); } } catch (\Exception $e) { // perhaps log an error and display a message to the user } return new HtmlResponse($this->template->render('album::add', ['form' => $this->form])); }
/** * @param ServerRequestInterface $request * @param ResponseInterface $response * @return ResponseInterface */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response) { try { $album = $this->albumService->getAlbum($request->getAttribute('id')); if ($request->getMethod() === 'POST') { $body = new Parameters($request->getParsedBody()); $del = $body->get('del', 'No'); if (strtolower($del) === 'yes') { $this->albumService->deleteAlbum($album); /** * @var Session $session */ $session = $request->getAttribute('session'); $session->getSegment('App\\Album')->setFlash('flash', ['type' => 'success', 'message' => sprintf('Successfully deleted album %s (%s)', $album->getTitle(), $album->getArtist())]); } // Redirect to list of albums return new RedirectResponse($this->router->generateUri('album.index')); } } catch (\Exception $e) { // do something useful } return new HtmlResponse($this->template->render('album::delete', compact('album'))); }
/** * Usage: {{ path('name', parameters) }} * * @param $name * @param array $parameters * @param bool $relative * @return string */ public function renderUri($name, $parameters = [], $relative = false) { return $this->router->generateUri($name, $parameters); }
/** * @param string $routeName * @param array $options * @return string */ public function __invoke($routeName, $options = []) { return $this->router->generateUri($routeName, $options); }