Skip to content

Instantly share code, notes, and snippets.

@PoTHuYJoHN
Created February 4, 2015 10:39
Show Gist options
  • Save PoTHuYJoHN/aeeace329f09033bede5 to your computer and use it in GitHub Desktop.
Save PoTHuYJoHN/aeeace329f09033bede5 to your computer and use it in GitHub Desktop.
SEO trait
<?php
abstract class Controller_Common extends Controller
{
use Traits_ItemSeo;
public function after()
{
$view->title = $this->getMetaTitle();
$view->description = $this->getMetaDescription();
$view->socialTitle = $this->getSocialTitle() ?: $this->getMetaTitle();
$view->footerDescription = $this->getFooterDescription();
}
}
<?php
/**
* Class Traits_ItemSeo
*/
trait Traits_ItemSeo {
/**
* @var <meta name="description"> tag value
*/
public $description;
/**
* @var <title> tag value
*/
public $title;
/**
* @var
*/
private $socialTitle;
/**
* @var
*/
private $_footerDescription;
/**
* @return mixed
*/
public function getFooterDescription()
{
return $this->_footerDescription;
}
/**
* @param mixed $footerDescription
*/
public function setFooterDescription($footerDescription)
{
$this->_footerDescription = $this->persistText($footerDescription);
}
/**
* @return mixed
*/
public function getSocialTitle()
{
return $this->socialTitle;
}
/**
* @param mixed $socialTitle
*/
public function setSocialTitle($socialTitle)
{
$this->socialTitle = $this->persistText($socialTitle);
}
/**
* Set <title> tag value
*/
protected function setMetaTitle($value)
{
$this->title = $this->persistText($value);
}
/**
* @return mixed
*/
protected function getMetaTitle()
{
return $this->title;
}
/**
* Set <meta name="description"> tag value
*/
protected function setMetaDescription($value)
{
$this->description = $this->persistText($value);
}
/**
* @return mixed
*/
protected function getMetaDescription()
{
return $this->description;
}
/**
* @param $text
*
* @return mixed
*/
private function persistText($text)
{
return str_replace('"', '\'', htmlspecialchars_decode($text, ENT_QUOTES));
}
/**
* Set title and meta description with pagination
* @param array $paginator
*/
public function setPaginationAttributes($paginator)
{
if($paginator['prev']) {
$this->setPaginationLink('prev', $paginator['prev']);
}
if($paginator['next']) {
$this->setPaginationLink('next', $paginator['next']);
}
$page = Request::get('page',false);
if($page) {
$this->setMetaTitle($this->getMetaTitle() . ' - Сторінка ' . $page);
$this->setMetaDescription($this->getMetaDescription() . ' - Сторінка ' . $page);
}
}
/**
* Helper method for pagination.
* @param string $type next|prev
* @param $url
*/
public function setPaginationLink($type = 'next', $url)
{
switch($type) {
case 'next':
$this->view->paginationNext = $url;
break;
case 'prev':
$this->view->paginationPrev = $url;
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment