PHP code example of edulazaro / laraindexnow

1. Go to this page and download the library: Download edulazaro/laraindexnow library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

edulazaro / laraindexnow example snippets


IndexNow::track(Post::class)
    ->url(fn (Post $post) => route('blog.show', $post->slug))
    ->when(fn (Post $post) => $post->status === PostStatus::Published);

// app/Providers/IndexNowServiceProvider.php

public function boot(): void
{
    IndexNow::track(Post::class)
        ->url(fn (Post $post) => route('blog.show', $post->slug))
        ->when(fn (Post $post) => $post->status === PostStatus::Published && $post->published_at?->isPast())
        ->ignoring(['views', 'last_viewed_at'])
        ->affects(fn (Post $post) => [route('blog.index')]);
}

    ->whenIn('status', ['published', 'featured'])              // one attribute, several values
    ->when(['status' => 'published', 'review' => 'correct'])   // several attributes
    ->whenBecame(['published' => true, 'status' => 'live'])

IndexNow::track(Post::class)
    ->url(fn (Post $post) => route('blog.show', $post->slug))
    ->whenBecame('published', true)      // it just flipped to true
    ->when('review', 'correct');         // and this holds right now

    ->when('published')            // published is true
    ->whenNot('spam')              // spam is false
    ->when('comment_count')        // has at least one
    ->when('comment_count', '>=', 5)

IndexNow::track(Game::class)
    ->url('games.show')
    ->whenNot('index_now_sent')
    ->when('published')
    ->sets('index_now_sent');

// A game page is worth indexing once it is approved and has playthroughs.
// Either half can be the one that arrives last, so declare both routes.

IndexNow::track(Game::class)
    ->url('games.show')
    ->whenLeft('playthroughs', 0)        // playthroughs arrived, already approved
    ->whenHolds('approved', true);

IndexNow::track(Game::class)
    ->url('games.show')
    ->whenBecame('approved', true)       // approval arrived, playthroughs already there
    ->when(fn (Game $game) => $game->playthroughs > 0);

    ->url('blog.show')                                   // route('blog.show', $post)
    ->url('https://example.com/about')                   // taken literally
    ->url('/about')                                      // taken literally
    ->url(fn (Post $post) => route('blog.show', $post->slug))

    ->affects('blog.index')
    ->affects(['blog.index', 'https://example.com/novedades'])
    ->affects(fn (Post $post) => [route('blog.category', $post->category)])

use EduLazaro\IndexNow\Policy;

class PostIndexNow extends Policy
{
    public function url(Model $post): ?string
    {
        return route('blog.show', $post->slug);
    }

    public function when(Model $post): bool
    {
        return $post->status === PostStatus::Published;
    }
}

IndexNow::track(Post::class, PostIndexNow::class);

Post::where('status', 'draft')->update(['status' => 'published']);

IndexNow::submit('https://example.com/page');        // buffered, sent by the queue
IndexNow::submit([$urlOne, $urlTwo]);

IndexNow::submitNow($url);                           // sent inline, returns the results
IndexNow::submitModel($post);                        // resolves the URL from the registration

IndexNow::withoutSubmissions(function () {
    Post::factory()->count(50_000)->create();
});

'keys' => [
    'example.com' => 'ecd5c0c8f2ad4b0e9a0d1f2c3b4a5968',
    'example.org' => '5f3a9d7e1b2c4a6d8e0f1a2b3c4d5e6f',
],

$fake = IndexNow::fake();

$post->update(['status' => 'published']);

$fake->assertSubmitted(route('blog.show', $post->slug));
$fake->assertSubmittedCount(2);
$fake->assertNothingSubmitted();
bash
php artisan vendor:publish --tag=indexnow-config
php artisan indexnow:key
bash
php artisan indexnow:key --file
bash
php artisan indexnow:verify
bash
php artisan indexnow:sync Post
bash
php artisan vendor:publish --tag=indexnow-migrations
php artisan migrate