wepesi / metadata

为您的网页生成元数据

v1.0.0 2023-03-21 23:22 UTC

This package is auto-updated.

Last update: 2024-09-30 02:10:37 UTC


README

元标签对于SEO至关重要,因为它们告诉搜索引擎页面是关于什么的。可以把它们看作是所有搜索引擎的第一印象。

元数据生成器可以帮助您为您的网页生成元数据,同时关注支持的平台。唯一需要做的是提供所需的信息。

安装

composer require wepesi/metadata

使用

首先需要调用structure静态方法,该方法提供了访问定义的方法的机会。

    use Wepesi\MetaData;
    $meta = MetaData::build();

要获取要显示的数据结构,可以使用方法

  • build():将返回类的实例。
  • toArray():将返回定义的字段的数组对象。
  • toHtml()generate:将返回可以添加到您网页中的HTML元标签。

如果未定义任何元素,则将返回空数组,构建将不带信息。

  use Wepesi\MetaData;
  MetaData::build()->toArray();
  /**
      array(0) {
      }
    **/

所有已知的元数据方法都已定义,包括:titlelangcoverauthordescriptionstypelinkfollowkeywordindexnofollownoIndexcanonical

生成元标签

generate()toHtml()方法可以帮助生成要在您的标签中显示的元数据标签。

    use Wepesi\MetaData;
    $meta= MetaData::build()
            ->title("Welcome To our Article")
            ->description("About Description of the article")
            ->lang("sw")
            ->type("article")
            ->link("https://www.domaine.com/about")
            ->cover("https://www.domaine.com/article-cover/cover.jpg")
            ->index()
            ->follow()
            ->toHtml();
    <!-- Open Graph data-->
<meta property="og:site_name" content="Wepesi"/>
<meta property="og:title" content="Welcome To our Article"/>
<meta property="og:description" content="About Description of the article"/>
<meta property="og:url" content="https://www.domaine.com/about"/>
<meta property="og:type" content="article"/>
<meta property="og:image:secure_url" content="https://www.domaine.com/article-cover/cover.jpg"/>
<meta property="og:local" content="sw"/>
<meta property="og:image:type" content="image/jpeg">
<!-- Size of image. Any size up to 300. Anything above 300px will not work in WhatsApp -->
<meta property="og:image:width" content="300">
<meta property="og:image:height" content="300">
<!-- Twitter MetaData -->
<meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="Welcome To our Article"/>
<meta name="twitter:description" content="About Description of the article"/>
<meta name="twitter:url" content="https://www.domaine.com/about"/>
<meta name="twitter:type" content="article"/>
<meta name="twitter:image" content="https://www.domaine.com/article-cover/cover.jpg"/>
<meta name="twitter:local" content="sw"/>
<meta name="twitter:site" content="">
<meta name="twitter:creator" content="">
<!-- Extra information -->
<meta name="mobile-web-app-capable" content="yes"/>
<meta name="apple-mobile-web-app-title" content="yes"/>
<meta name="robots" content="index,follow">
<link rel="canonical" href=""/>"

更多关于元数据方法的信息

描述

为了构建有用的元数据,方法被精心设计以帮助您轻松使用。

title方法

标题标签是第一个指定您的网页是关于什么的HTML元素。标题标签对SEO和访客都很重要,因为它们出现在搜索引擎结果页面(SERP)和浏览器标签中。

    use Wepesi\MetaData;
    $structure= MetaData::build()->title("Welcome To Wepesi")->toArray();
    /**
    array(1) {
        ["title"]=>"Welcome To Wepesi"
    }
    */

lang方法

meta lang是一个帮助设置网页语言的HTML元素。

    use Wepesi\MetaData;
    $structure= MetaData::build()
                ->title("Welcome To Wepesi")
                ->lang("fr")
                ->toArray();
    /**
    array(1) {
      ["title"]=>"Welcome To Wepesi"
      ["lang"]=>"fr"
    }
    */

cover方法

meta cover是一个帮助设置在将要显示的卡片上播放的图像的HTML元素。支持的图像格式为jpgpng

    use Wepesi\MetaData;
    $structure= MetaData::build()
                ->title("Welcome To Wepesi")
                ->lang("en")
                ->cover("https://www.domaine.com/cover.jpg")
                ->toArray();
    //array(1) {
    //  ["title"]=>"Welcome To Wepesi"
    //  ["lang"]=>"en"
    //  ["cover"]=>"https://www.domaine.com/cover.jpg"
    // }

author方法

meta author是一个帮助提供有关作者更多详细信息的HTML元素。

    use Wepesi\MetaData;
    $structure= MetaData::build()
                ->title("Welcome To Wepesi")
                ->author("Wepesi.")
                ->toArray();
    //array(1) {
    //  ["title"]=>"Welcome To Wepesi"
    //  ["author"]=>"Wepesi"
    // }

description方法

meta description是一个总结您的网页内容的HTML元素。搜索引擎通常在搜索结果中显示标题标签下面的元描述。

    use Wepesi\MetaData;
    $structure= MetaData::build()
                ->title("Welcome To Wepesi")
                ->description("Search engines typically show the meta description in search results below your title tag.")
                ->lang("fr")
                ->cover("https://www.domaine.com/cover.jpg")
                ->toArray();
    //array(1) {
    //  ["title"]=>"Welcome To Wepesi",
    //  ["description"]=>"Search engines typically show the meta description in search results below your title tag.",
    //  ["lang"]=>"fr",
    //  ["cover"]=>"https://www.domaine.com/cover.jpg",
    // }

type方法

类型帮助定义它是一个关于articlewebsite还是blog的内容,这里有一些元数据类型{article,blog,...}

    use Wepesi\MetaData;
    $structure= MetaData::build()
                ->title("Welcome To Wepesi")
                ->description("Search engines typically show the meta description in search results below your title tag.")
                ->lang("sw")
                ->type("article")
                ->toArray();
    //array(1) {
    //  ["title"]=>"Welcome To Wepesi",
    //  ["description"]=>"Search engines typically show the meta description in search results below your title tag.",
    //  ["lang"]=>"sw",
    //  ["type"]=>"article",
    // }

link方法

link方法帮助定义是网站链接还是文章链接用于重定向。如果是文章,您应提供博客文章的链接,以便直接到达文章。

    use Wepesi\MetaData;
    $structure= MetaData::build()
                ->title("Welcome To our Article")
                ->description("Description of the article")
                ->lang("sw")
                ->type("article")
                ->link("https://www.domaine.com/article/welcom-to-wepesi")
                ->cover("https://www.domaine.com/article-cover/cover.jpg")
                ->toArray();
    //array(1) {
    //  ["title"]=>"Welcome To our Article",
    //  ["description"]=>"Description of the article",
    //  ["lang"]=>"sw",
    //  ["type"]=>"article",
    //  ["link"]=>"https://www.domaine.com/article/welcom-to-wepesi",
    //  ["cover"]=>"https://www.domaine.com/article-cover/cover.jpg",
    // }

机器人元标签

列出要应用的头标签,例如:follow,index,nofollow FOLLOW:搜索引擎爬虫将跟随该网页上的所有链接,INDEX:搜索引擎爬虫将索引整个网页。 NOFOLLOW搜索引擎爬虫将不会跟随该页面以及该页面上的任何链接。NOINDEX搜索引擎爬虫将不会索引该网页。

请使用以下语法为您的robots元标签:只有2个robots标签可以使用

    use Wepesi\MetaData;
    $structure= MetaData::build()
                ->title("Welcome To our Article")
                ->description("About Description of the article")
                ->lang("sw")
                ->type("article")
                ->link("https://www.domaine.com/about")
                ->cover("https://www.domaine.com/article-cover/cover.jpg")
                ->index()
                ->follow()
                ->toArray();
    //array(1) {
    //  ["title"]=>"Welcome To our Article",
    //  ["description"]=>"About Description of the article",
    //  ["lang"]=>"sw",
    //  ["type"]=>"article",
    //  ["link"]=>"https://www.domaine.com/article/welcom-to-wepesi",
    //  ["cover"]=>"https://www.domaine.com/article-cover/cover.jpg",
    //  ["tags"]=>["index","follow"],
    // }

如果您不需要索引和跟随页面,请使用

    use Wepesi\MetaData;
    $structure= MetaData::build()
                ->title("Welcome To our Article")
                ->description("About Description of the article")                
                ->noindex()
                ->nofollow()
                ->toArray();
    //array(1) {
    //  ["title"]=>"Welcome To our Article",
    //  ["description"]=>"About Description of the article",
    //  ["tags"]=>["noindex","nofollow"],
    // }

查看示例输出

<meta name="robots" content="index, follow"> Means index and follow this webpage.
<meta name="robots" content="noindex, nofollow"> Means not to index or not to follow this webpage.

关键词方法

它用于添加关键词,这将有助于搜索引擎轻松映射您的网站。如果您有一个关键词,它将作为字符串参数传递;否则,您可以通过传递一个包含多个关键词的数组来实现。

    use Wepesi\MetaData;
    $structure= MetaData::build()
                ->title("Welcome To our Article")
                ->description("About Description of the article")                
                ->keywords(["HTML","CSS","JavaScript"])
                ->toArray();
    //array(1) {
    //  ["title"]=>"Welcome To our Article",

    //  ["description"]=>"About Description of the article",
    //  ["keywords"]=>["HTML","CSS","JavaScript"]
    // }

规范方法

它用于指示此网页有其他版本。通过在代码中实现规范标签,您的网站会告知搜索引擎该URL是主页面,并且搜索引擎不应索引其他页面。

    use Wepesi\MetaData;
    $structure = MetaData::build()
                ->title("Welcome To our Article")
                ->description("About Description of the article")                
                ->canonical("https://www.domaine.com")
                ->toArray();
    //array(1) {
    //  ["title"]=>"Welcome To our Article",
    //  ["description"]=>"About Description of the article",
    //  ["canonical"]=>"https://www.domaine.com",
    // }