/** * Generate the website robots.txt */ public function indexAction() { $response = new Response(); $expireDate = new \DateTime(); $expireDate->modify('+1 month'); $response->setExpires($expireDate); $response->setHeader('Content-Type', "text/plain; charset=UTF-8"); $baseUrl = rtrim($this->config->site->url, '/'); $content = <<<EOL User-agent: * Allow: / Sitemap: {$baseUrl}/sitemap EOL; $response->setContent($content); return $response; }
/** * Generate the website sitemap */ public function indexAction() { $response = new Response(); $expireDate = new \DateTime(); $expireDate->modify('+1 day'); $response->setExpires($expireDate); $response->setHeader('Content-Type', "application/xml; charset=UTF-8"); $sitemap = new \DOMDocument("1.0", "UTF-8"); $urlset = $sitemap->createElement('urlset'); $urlset->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9'); $urlset->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'); $baseUrl = $this->config->site->url; $url = $sitemap->createElement('url'); $url->appendChild($sitemap->createElement('loc', $baseUrl)); $url->appendChild($sitemap->createElement('changefreq', 'daily')); $url->appendChild($sitemap->createElement('priority', '1.0')); $urlset->appendChild($url); $karmaSql = 'number_views + ' . '((IF(votes_up IS NOT NULL, votes_up, 0) - IF(votes_down IS NOT NULL, votes_down, 0)) * 4) + ' . 'number_replies'; $parametersPosts = ['conditions' => 'deleted != 1', 'columns' => "id, slug, modified_at, {$karmaSql} AS karma", 'order' => 'karma DESC']; $posts = Posts::find($parametersPosts); $parametersKarma = ['column' => $karmaSql, 'conditions' => 'deleted != 1']; $karma = Posts::maximum($parametersKarma); $modifiedAt = new \DateTime(); $modifiedAt->setTimezone(new \DateTimeZone('UTC')); foreach ($posts as $post) { $modifiedAt->setTimestamp($post->modified_at); $postKarma = $post->karma / ($karma + 100); $url = $sitemap->createElement('url'); $href = trim($baseUrl, '/') . '/discussion/' . $post->id . '/' . $post->slug; $url->appendChild($sitemap->createElement('loc', $href)); $valuePriority = $postKarma > 0.7 ? sprintf("%0.1f", $postKarma) : sprintf("%0.1f", $postKarma + 0.25); $url->appendChild($sitemap->createElement('priority', $valuePriority)); $url->appendChild($sitemap->createElement('lastmod', $modifiedAt->format('Y-m-d\\TH:i:s\\Z'))); $urlset->appendChild($url); } $sitemap->appendChild($urlset); $response->setContent($sitemap->saveXML()); return $response; }
public function setExpires(\Datetime $datetime) { return parent::setExpires($datetime); }
/** * Cache the response 1 year (31536000 sec) */ protected function cacheResponse(Response $response) { $response->setHeader('Cache-Control', 'public, max-age=31536000, s-maxage=31536000'); $response->setExpires(new \DateTime('+1 year')); return $response; }