Add link() and setCanonicalUrl() methods to the WebAppView

These make it easier for controllers to define relationships from
the current to other pages, which is important for SEO mostly.
This commit is contained in:
Franz Liedke 2017-03-13 18:08:32 +01:00
parent 5d62231004
commit 231d018de5
No known key found for this signature in database
GPG Key ID: 9A0231A879B055F4

View File

@ -107,6 +107,13 @@ class WebAppView
*/
protected $foot = [];
/**
* A map of <link> tags to be generated.
*
* @var array
*/
protected $links = [];
/**
* @var CompilerInterface
*/
@ -219,6 +226,31 @@ class WebAppView
$this->foot[] = $string;
}
/**
* Configure a <link> tag.
*
* @param string $relation
* @param string $target
*/
public function link($relation, $target)
{
$this->links[$relation] = $target;
}
/**
* Configure the canonical URL for this page.
*
* This will signal to search engines what URL should be used for this
* content, if it can be found under multiple addresses. This is an
* important tool to tackle duplicate content.
*
* @param string $url
*/
public function setCanonicalUrl($url)
{
$this->link('canonical', $url);
}
/**
* Set a variable to be preloaded into the app.
*
@ -271,7 +303,7 @@ class WebAppView
$view->cssUrls = $this->buildCssUrls($baseUrl);
$view->jsUrls = $this->buildJsUrls($baseUrl);
$view->head = implode("\n", $this->head);
$view->head = $this->buildHeadContent();
$view->foot = implode("\n", $this->foot);
return $view->render();
@ -337,6 +369,17 @@ class WebAppView
}, array_filter($files));
}
protected function buildHeadContent()
{
$html = implode("\n", $this->head);
foreach ($this->links as $rel => $href) {
$html .= "\n<link rel=\"$rel\" href=\"$href\" />";
}
return $html;
}
/**
* @return CompilerInterface
*/