定制 elfeffe/laravel-google-indexing 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

elfeffe/laravel-google-indexing

最新稳定版本:v1.0.0

Composer 安装命令:

composer require elfeffe/laravel-google-indexing

包简介

Index Laravel website in Google via Indexing API

README 文档

README

Submit URLs to Google's Indexing API from Laravel and track successful submissions in google_indexing_records.

This package is useful for sites that are eligible for the Google Indexing API and want:

  • direct URL update/delete requests
  • helper methods for quota-aware indexing
  • a reusable GoogleIndexable trait for models
  • persistent records of successful submissions

Requirements

  • PHP 8.4+
  • Laravel 12 or 13
  • a Google service account configured for the Indexing API

Important note

Google only allows the Indexing API for specific content types, such as job posting and livestream pages. Check the official docs before using it broadly:

Google Indexing API docs

Installation

composer require elfeffe/laravel-google-indexing:^1.0

Publish the config:

php artisan vendor:publish --tag=laravel-google-indexing-config

Publish the migration:

php artisan vendor:publish --tag=laravel-google-indexing-migrations

The package now reuses an existing published create_google_indexing_records_table migration if one is already present, so republishing does not create duplicate migration files.

Configuration

By default the package expects the Google auth JSON at:

storage_path('google_auth_config.json')

You can override it in config/laravel-google-indexing.php:

return [
    'google' => [
        'auth_config' => storage_path('google_auth_config.json'),
        'scopes' => [
            'https://www.googleapis.com/auth/indexing',
        ],
    ],
];

You may also pass a JSON string or array directly when instantiating the service.

Basic usage

Facade

use Elfeffe\LaravelGoogleIndexing\Facades\LaravelGoogleIndexingFacade as LaravelGoogleIndexing;

LaravelGoogleIndexing::update('https://example.com/page');
LaravelGoogleIndexing::delete('https://example.com/page');
LaravelGoogleIndexing::status('https://example.com/page');

Direct service usage

use Elfeffe\LaravelGoogleIndexing\LaravelGoogleIndexing;

$googleIndexing = new LaravelGoogleIndexing();

$googleIndexing->update('https://example.com/page');

Custom auth config

use Elfeffe\LaravelGoogleIndexing\LaravelGoogleIndexing;

$googleIndexing = LaravelGoogleIndexing::forAuthConfig(
    storage_path('my-google-service-account.json')
);

Model indexing

If a model exposes a canonical URL, you can use the GoogleIndexable trait.

use Elfeffe\LaravelGoogleIndexing\Traits\GoogleIndexable;
use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    use GoogleIndexable;

    public function getGoogleIndexingUrl(): string
    {
        return route('articles.show', $this);
    }
}

Then:

use Elfeffe\LaravelGoogleIndexing\LaravelGoogleIndexing;

$service = new LaravelGoogleIndexing();
$service->updateModel($article);

Helper usage

The helper wraps the service with daily quota tracking based on successful submissions stored in google_indexing_records.

use Elfeffe\LaravelGoogleIndexing\Helpers\IndexingHelper;

$helper = app(IndexingHelper::class);

$helper->indexUrl('https://example.com/page');
$helper->indexUrls([
    'https://example.com/page-1',
    'https://example.com/page-2',
]);
$helper->indexModel($article);

Quota helpers

$helper->isQuotaExceeded();
$helper->getRemainingQuota();

Using the trait:

Article::getTodayIndexingCount();
Article::getRemainingDailyQuota();
Article::query()->needsGoogleIndexing(30)->get();

Stored records

Successful requests are stored in google_indexing_records with:

  • url
  • status
  • sent_at
  • response_data
  • error_message
  • optional morph relation via indexable_type / indexable_id

This lets you avoid unnecessary resubmissions and track recent indexing activity.

Exceptions

Quota errors throw:

Elfeffe\LaravelGoogleIndexing\Exceptions\GoogleQuotaExceededException

You should catch it if you are bulk processing URLs.

Credits

License

MIT. See LICENSE.md.

统计信息

  • 总下载量: 52
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-03