kokjan/frontend/components/MetaComponent.php

63 lines
1.7 KiB
PHP
Raw Permalink Normal View History

2026-02-25 06:59:34 +00:00
<?php
2026-02-25 06:59:34 +00:00
namespace frontend\components;
2026-02-25 06:59:34 +00:00
use yii\base\Component;
use Yii;
class MetaComponent extends Component
{
2026-02-25 06:59:34 +00:00
public $keywords = 'องค์การบริหารส่วนตำบลบอน,อบต.บอน';
public $description = 'องค์การบริหารส่วนตำบลบอน';
public $image = 'https://kokjan.go.th/img/nopic.png';
2026-02-25 06:59:34 +00:00
public function displaySeo()
{
2026-02-25 06:59:34 +00:00
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)
{
2026-02-25 06:59:34 +00:00
$first_img = '';
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $content, $matches);
$first_img = $matches[1][0];
if (empty($first_img)) {
$first_img = 'https://kokjan.go.th/img/nopic.png';
2026-02-25 06:59:34 +00:00
}
return $first_img;
}
}