think.studio/nova-seo-entity

用于管理SEO数据的关联实体。

4.4.0 2023-09-03 09:13 UTC

This package is auto-updated.

Last update: 2024-09-10 14:01:57 UTC


README

Packagist License Packagist Version Total Downloads Build Status Code Coverage Scrutinizer Code Quality

将SEO数据添加到任何模型关系。

安装

您可以通过composer安装此包

composer require think.studio/nova-seo-entity

# optional publish configs
php artisan vendor:publish --provider="NovaSeoEntity\ServiceProvider" --tag="config"
# as current package wrap "artesaos/seotools" package, will be useful publish internal config:
php artisan vendor:publish --provider="Artesaos\SEOTools\Providers\SEOToolsServiceProvider"

# publish translations
php artisan vendor:publish --provider="NovaSeoEntity\ServiceProvider" --tag="lang"

用法

添加SEO表

php artisan make:migration create_cms_seo_table
public function up()
{
    Schema::create(config('nova-seo-entity.table'), function (Blueprint $table) {
        \NovaSeoEntity\Database\MigrationHelper::defaultColumns($table);
    });
}

public function down()
{
    Schema::dropIfExists(config('nova-seo-entity.table'));
}

修改模型

class Article extends Model implements \NovaSeoEntity\Contracts\WithSeoEntity
{
    use \NovaSeoEntity\Models\Traits\HasSeoEntity;
    // ...
    
    /**
     * Example how set default value for nova "creation" screen
     */
    public function getNewInstanceSeoValueForDescription( ): ?string {
        return Str::limit( WysiwygHelper::html2Text( $this->content ), 150 );
    }
    
    /**
     * Override canonical value if not set
     */
    public function getSEOCanonicalFieldValue( mixed $value ): mixed {
        return $value ?: ($this->slug ? route( 'front.article.single', $this->slug ) : null);
    }

}

修改nova资源

向您的资源添加新字段

MorphOne::make('SEO', 'seo_info', SEOInfo::class),

修改应用服务提供商

您可以从包中添加资源或扩展您的应用程序。

// NovaServiceProvider.php
use NovaSeoEntity\Nova\Resources\SEOInfo;

public function boot() {
    // ...

    SEOInfo::morphToTypes([
        \App\Nova\Resources\CMS\Article::class,
        \App\Nova\Resources\CMS\Page::class
        // ...
    ]);

    parent::boot();
}

protected function resources() {
    parent::resources();
    // ...
    Nova::resources( [
        SEOInfo::class,
        // ...
    ] );
}

添加相关文件系统磁盘(或更改配置文件中的默认磁盘)

'cms-images'  => [
    'driver'     => 'local',
    'root'       => storage_path('app/public/cms-images'),
    'url'        => env('APP_URL').'/storage/cms-images',
    'visibility' => 'public',
],

显示元数据

<head>
    {!! \Artesaos\SEOTools\Facades\SEOTools::generate(!config('app.debug')) !!}
</head>

实现

$article = Article::find($articleId);

// Get seoinfo with default value
$article?->seo_info_forced->seoPrepare();

// Get seoinfo without defaults
// $article?->seo_info?->seoPrepare();

有用的链接

Facebook: 共享 , 最佳实践. Facebook 调试器

Twitter: 摘要卡 , 带大图的摘要卡 . Twitter 验证器

JsonLd: 介绍 , 建议 , 图像许可 , 示例

致谢

  • Think Studio