}; // Replace notFoundHandler to use an API response $container['notFoundHandler'] = function ($c) { return function ($request, $response) use($c) { return $c['response']->withHeader('Content-Type', 'application/json')->write('{ message: "Matching API call not found." }'); }; }; // Replace the errorHandler to use an API response $container['errorHandler'] = function ($c) { return function ($request, $response, $exception) use($c) { $c['logger']->addError('Server error', $exception->getTrace()); return $c['response']->withStatus(500)->withHeader('Content-Type', 'application/json')->write('{ message: "Internal Server Error", error: "' . $exception->getMessage() . '" }'); }; }; // Routes ending in '/' use route without '/' $app->add(function ($request, $response, $next) { $uri = $request->getUri(); $path = $uri->getPath(); if (strlen($path) > 1 && substr($path, -1) === '/') { $path = substr($path, 0, -1); } if ($uri->getPath() !== $path) { return $next($request->withUri($uri->withPath($path)), $response); } return $next($request, $response); }); // Create RSS on first run if (!file_exists('../rss/rss.xml')) { $rss = new RssGenerator(); $rss->updateRss(); }
public function unpublishPost($request, $response, $args) { $status = $this->secureRoute($request, $response); if ($status !== 200) { return $this->jsonResponse($response, $status); } $user = Auth::GetUser($request); $post = $this->loadPost($args['id']); if (!$post) { return $this->jsonResponse($response, 400); } if ($post->user_id !== $user->id && !$user->is_admin) { $this->apiJson->addAlert('error', 'You cannot unpublish this post.'); return $this->jsonResponse($response, 403); } $post->is_published = false; $post->publish_date = null; R::store($post); $rss = new RssGenerator(); $rss->updateRss(); $this->apiJson->setSuccess(); $this->apiJson->addAlert('success', 'Post ' . $post->title . ' unpublished.'); $this->apiJson->addData($post->export()); return $this->jsonResponse($response); }