在Yii2中为每个页面添加SEO标签

时间:2019-01-24 07:57:02

标签: html yii2 phpunit metadata meta-tags

我对这个话题很陌生。我有一个使用Yii框架的PHP编写的网站。在我的自定义CDN中,我通过它注入了内容,我的开发人员设计了一个部分,可以为各个页面分配不同的Meta标签或SEO标签。我的问题是该机制不起作用,实际上,当我说出元描述时,它在所有页面上都一样。我无法访问我的开发者来更正此问题。在下面,我提供了layout和Metadata.php的代码。 如前所述,我是这个主题的新手,不知道应该提交哪些代码进行考虑。如果需要更多代码,请询问我。

我的目标是更正我的后端条目(“元数据列表”部分),以使每个页面都具有我提供的唯一元标记。

$metadata = \common\models\base\Metadata::find()->where(['page_routing' => 
Yii::$app->controller->id."/". Yii::$app->controller->action->id ])->one();
if ( empty($metadata ) )
$metadata = \common\models\base\Metadata::find()->where(['id' => 1 ])- 
>one();
?>

<?php $this->beginPage() ?>

<!DOCTYPE html>

<html lang="<?= Yii::$app->language ?>">

<head>
<meta charset="<?= Yii::$app->charset ?>">
<?php echo Html::csrfMetaTags() ?>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1, 
maximum-scale=1, user-scalable=no"/>
<meta name="robots" content="<?=$metadata->meta_robots?>"/>
<link rel="canonical" href="https://physexams.com/<?=Yii::$app->controller- 
>id."/". Yii::$app->controller->action->id?>" />
<meta property="og:image" content="<?=$metadata->og_image?>"/>
<meta property="og:title" content="<?=$metadata->og_title?>‌"/>
<meta property="og:description" content="<?=$metadata->og_description?>"/>
<meta name="twitter:image" content="<?=$metadata->twitter_image?>"/>
<meta property="og:locale" content="en_CA" />
<meta name="googlebot" content="index" />
<meta name="revisit-after" content="1 Days" />
<meta name="doc-class" content="Living Document" />
<meta name="language" content="english">
<?= Html::csrfMetaTags() ?>
<link rel="publisher" href="<?=$metadata->publisher?>" />
<title><?= Html::encode($this->title) ?></title>

<?php $this->head() ?>

在上面,为简短起见,我剪切了其他不需要的元标记并提交了部分代码。如果需要完整的代码,我可以提交。 和Metadata.php的代码     

namespace common\models\base;

use Yii;

/**
 * This is the base-model class for table "metadata".
 *
 * @property integer $id
 * @property string $page_routing
 * @property string $title
 * @property string $meta_description
 * @property string $author
 * @property string $author_link
 * @property string $publisher
 * @property string $og_title
 * @property string $og_type
 * @property string $og_image
 * @property string $og_url
 * @property string $og_description
 * @property string $twitter_card
 * @property string $twitter_url
 * @property string $twitter_title
 * @property string $twitter_description
 * @property string $twitter_image
 * @property string $meta_keywords
 * @property string $meta_robots
 * @property string $meta_copyright
 * @property string $itemprop
 * @property string $Search_Display
 */
 class Metadata extends \yii\db\ActiveRecord
 {



/**
 * @inheritdoc
 */
public static function tableName()
{
    return 'metadata';
}

/**
 * @inheritdoc
 */
public function rules()
{
    return [
        [['meta_description', 'meta_keywords', 'Search_Display'], 
'string'],
        [['page_routing', 'title', 'author', 'author_link', 'publisher', 
'og_title', 'og_type', 'og_image', 'og_url', 'og_description', 
'twitter_url', 'twitter_title', 'twitter_description', 'twitter_image', 
'meta_robots', 'meta_copyright'], 'string', 'max' => 1000],
        [['twitter_card'], 'string', 'max' => 255],
        [['itemprop'], 'string', 'max' => 2000]
    ];
}

/**
 * @inheritdoc
 */
public function attributeLabels()
{
    return [
        'id' => 'ID',
        'page_routing' => 'Page Routing',
        'title' => 'Title',
        'meta_description' => 'Meta Description',
        'author' => 'Author',
        'author_link' => 'Author Link',
        'publisher' => 'Publisher',
        'og_title' => 'Og Title',
        'og_type' => 'Og Type',
        'og_image' => 'Og Image',
        'og_url' => 'Og Url',
        'og_description' => 'Og Description',
        'twitter_card' => 'Twitter Card',
        'twitter_url' => 'Twitter Url',
        'twitter_title' => 'Twitter Title',
        'twitter_description' => 'Twitter Description',
        'twitter_image' => 'Twitter Image',
        'meta_keywords' => 'Meta Keywords',
        'meta_robots' => 'Meta Robots',
        'meta_copyright' => 'Meta Copyright',
        'itemprop' => 'Itemprop',
        'Search_Display' => 'Search  Display',
    ];
}
}

配置Main.php的代码:

return [
'id' => 'app-frontend',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log', 'thumbnail'],
'controllerNamespace' => 'frontend\controllers',
//'defaultRoute' => '/site/index',

'components' => [
    'request' => [
        'baseUrl' => $baseUrl,
    ],
    'urlManager' => [
        'baseUrl' => $baseUrl,
        'class' => 'yii\web\UrlManager',

        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'enableStrictParsing' => false,
        'rules' => [
            'home' => 'site/index' ,
            '/' => 'site/index', 
            'frontend/web' => 'site/index',
            'index' => 'site/index',
            'site' => 'site/index',
            'site/login' => 'site/login' ,
            'site/contact' => 'site/contact' ,
            'site/about' => 'site/about' ,
            'site/faq' => 'site/faq' ,
            'profile/logout' => 'profile/logout',
            'profile' => 'profile/index',
            'lesson/<lesson:\w+>/<id:\d+>' => 'courses/internal',
            'lesson' => 'courses/internal',

            'course/<cat:\w+>/<id:\d+>' => 'courses/index',
            'courses' => 'courses/index',

            'exam/<cat:\w+>/<id:\d+>' => 'exam/internal',
            'exam' => 'exam/index',

            'ticket' => 'ticket/index',



            'flashcard/<cat:\w+>/<id:\d+>' => 'blog/internal',
            'blog/<cat:\w+>/<id:\d+>' => 'blog/cat',
            'blog' => 'blog/index',

            '<controller:\w+>/<action:\w+>/<id:\d+>' => 
'<controller>/<action>',


        ],
        ],
    'user' => [
        'identityClass' => 'common\models\User',
        'enableAutoLogin' => true,
    ],
    'log' => [
        'traceLevel' => YII_DEBUG ? 3 : 0,
        'targets' => [
            [
                'class' => 'yii\log\FileTarget',
                'levels' => ['error', 'warning'],
            ],
        ],
    ],
    'errorHandler' => [
        'errorAction' => 'site/error',
    ],

    'thumbnail' => [
        'class' => 'himiklab\thumbnail\EasyThumbnail',
        'cacheAlias' => 'assets/gallery_thumbnails',
    ],
    'assetManager' => [
        'class' => 'yii\web\AssetManager',
        'bundles' => [
                    'yii\web\JqueryAsset' => [
                        'js' => [
                            'jquery.min.js'
                        ]
                    ],

                    'yii\bootstrap\BootstrapAsset' => [
                        'css' => [
                           'css/bootstrap.min.css',
                        ]
                    ],

        ],
    ],
],
'params' => $params,
];

0 个答案:

没有答案