kokjan/frontend/components/MetaComponent.php

59 lines
1.7 KiB
PHP

<?php
namespace frontend\components;
use yii\base\Component;
use Yii;
class MetaComponent extends Component{
public $keywords = 'องค์การบริหารส่วนตำบลบอน,อบต.บอน';
public $description = 'องค์การบริหารส่วนตำบลบอน';
public $image = 'https://bon.go.th/img/nopic.png';
public function displaySeo(){
Yii::$app->view->registerMetaTag([
'name' => 'description',
'content' => $this->description,
]);
Yii::$app->view->registerMetaTag([
'name' => 'keywords',
'content' => $this->keywords,
]);
Yii::$app->view->registerMetaTag([
'name' => 'og:description',
'content' => $this->description,
]);
Yii::$app->view->registerMetaTag([
'name' => 'og:type',
'content' => 'websites',
]);
Yii::$app->view->registerMetaTag([
'name' => 'og:image',
'content' => $this->image,
]);
Yii::$app->view->registerMetaTag([
'name' => 'twitter:description',
'content' => $this->description,
]);
Yii::$app->view->registerMetaTag([
'name' => 'twitter:image',
'content' => $this->image,
]);
}
public function getFirstImage($content) {
$first_img = '';
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $content, $matches);
$first_img = $matches[1][0];
if(empty($first_img)) {
$first_img = 'https://bon.go.th/img/nopic.png';
}
return $first_img;
}
}