PHP code example of zarbinco / zarbin-seo
1. Go to this page and download the library: Download zarbinco/zarbin-seo 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/ */
zarbinco / zarbin-seo example snippets
'routes' => [
'home' => [
'title' => 'Home',
'description' => 'Welcome to our website.',
'canonical' => 'https://example.com',
'robots' => 'index, follow',
'sitemap' => true,
],
],
seo()->route('home')->render();
public function show(Post $post)
{
seo()->for($post);
return view('posts.show', compact('post'));
}
seo()
->title('About Us')
->description('Learn more about our company.')
->canonical(route('about'))
->render();
use Illuminate\Database\Eloquent\Model;
use Zarbin\Seo\Concerns\HasSeo;
use Zarbin\Seo\Contracts\Seoable;
class Post extends Model implements Seoable
{
use HasSeo;
public function seoTitle(?string $locale = null): ?string
{
return $this->title;
}
public function seoDescription(?string $locale = null): ?string
{
return $this->excerpt;
}
public function seoCanonicalUrl(?string $locale = null): ?string
{
return route('posts.show', $this);
}
}
'models' => [
App\Models\Post::class => [
'title' => 'title',
'description' => ['excerpt', 'content'],
'image' => 'cover_image_url',
'route' => 'posts.show',
'route_key' => 'slug',
'type' => 'Article',
],
],
class ProductHolder extends Model implements Seoable
{
use HasSeo;
public function seoTitle(?string $locale = null): ?string
{
return $this->title ?: 'Products';
}
public function seoType(?string $locale = null): ?string
{
return 'CollectionPage';
}
}
'routes' => [
'home' => [
'title' => 'Home',
'description' => 'Welcome to our website.',
'canonical' => 'https://example.com',
'schema' => 'WebPage',
'sitemap' => true,
],
],
seo()->route('home')->render();
'localization' => [
'enabled' => true,
'locales' => ['fa', 'en'],
'default_locale' => 'fa',
'url_strategy' => 'prefixed_all',
'route_parameter' => 'locale',
'missing_translation_strategy' => 'hide',
'generate_hreflang' => true,
'x_default' => 'fa',
],
use Zarbin\Seo\Contracts\LocalizableSeo;
class Post extends Model implements Seoable, LocalizableSeo
{
use HasSeo;
public function seoLocales(): array
{
return ['fa', 'en'];
}
public function hasSeoLocale(string $locale): bool
{
return filled($this->getTranslation('title', $locale));
}
public function seoUrlForLocale(string $locale): ?string
{
return route('posts.show', ['locale' => $locale, 'post' => $this->slug]);
}
}
'localization' => [
'enabled' => true,
'locales' => ['fa', 'en'],
'default_locale' => 'fa',
'url_strategy' => 'prefixed_all',
'route_parameter' => 'locale',
],
'sitemap' => [
'base_url' => 'https://example.com',
'content_type' => 'application/xml; charset=UTF-8',
'localized_paths' => [
'fa' => 'sitemap-fa.xml',
'en' => 'sitemap-en.xml',
],
'
'sitemap' => [
'
'sitemap' => [
'content_type' => 'text/xml; charset=UTF-8',
],
'routes' => [
'products.fa' => [
'locale' => 'fa',
'canonical' => 'https://example.com/fa/products',
'sitemap' => true,
],
'products.en' => [
'locale' => 'en',
'canonical' => 'https://example.com/en/products',
'sitemap' => true,
],
],
'routes' => [
'home' => [
'title' => 'Home',
'canonical' => 'https://example.com',
'sitemap' => true,
'priority' => 1.0,
'change_frequency' => 'daily',
],
],
'models' => [
App\Models\Post::class => [
'route' => 'posts.show',
'route_key' => 'slug',
'sitemap' => true,
'sitemap_source' => App\Seo\PostSeoSitemapSource::class,
'priority' => 0.7,
'change_frequency' => 'weekly',
],
],
public function shouldBeInSitemap(?string $locale = null): bool
{
return $this->published;
}
public function sitemapUrl(?string $locale = null): ?string
{
return route('posts.show', $this);
}
public function sitemapLastModified(?string $locale = null): mixed
{
return $this->updated_at;
}
'features' => [
'database_overrides' => true,
],
'database' => [
'enabled' => true,
],
seo()->saveOverride($post, [
'title' => 'Custom SEO title',
'description' => 'Custom SEO description',
'canonical' => 'https://example.com/custom-post',
'robots' => ['index', 'follow'],
], 'fa');
seo()->saveOverride('home', [
'title' => 'Custom homepage title',
'description' => 'Custom homepage description',
], 'en');
use Zarbin\Seo\Concerns\HasSeoMeta;
class Post extends Model implements Seoable
{
use HasSeo;
use HasSeoMeta;
}
'features' => [
'database_overrides' => true,
'ui' => true,
],
'database' => [
'enabled' => true,
],
'ui' => [
'enabled' => true,
'route_enabled' => true,
'path' => 'admin/seo',
'middleware' => ['web', 'auth'],
'gate' => 'viewZarbinSeo',
'preset' => 'unstyled',
'form_layout' => [
'mode' => 'responsive',
],
'layout' => [
'mode' => 'standalone',
'view' => null,
'section' => 'content',
'title_section' => 'title',
],
'direction' => [
'mode' => 'auto',
'rtl_locales' => ['fa', 'ar', 'he', 'ur', 'ku', 'ckb', 'ps', 'sd', 'yi'],
'fallback' => 'ltr',
],
'components' => [
'global_aliases' => false,
'alias_prefix' => 'zarbin-seo',
],
'completion' => [
'
'ui' => [
'layout' => [
'mode' => 'host',
'view' => 'layouts.admin',
'section' => 'content',
'title_section' => 'title',
],
],
'ui' => [
'components' => [
'global_aliases' => true,
],
],
use Zarbin\Seo\Support\SeoOverridePayload;
$product->fill($request->validated());
$product->save();
seo()->saveOverride($product, SeoOverridePayload::fromRequest($request), 'fa');
use Zarbin\Seo\Support\SeoFieldValues;
use Zarbin\Seo\Support\SeoOverridePayload;
public array $zarbinSeo = [];
public function mount(?int $id = null): void
{
if ($id) {
$this->product = Product::findOrFail($id);
$this->zarbinSeo = app(SeoFieldValues::class)->forSource($this->product, app()->getLocale());
}
}
public function save(): void
{
$this->product->save();
seo()->saveOverride(
$this->product,
SeoOverridePayload::normalize($this->zarbinSeo),
app()->getLocale()
);
}
'ui' => [
'preset' => 'utility',
'form_layout' => [
'mode' => 'responsive',
],
],
'ui' => [
'preset' => 'custom',
'classes' => [
'field_input' => 'project-field project-field-input',
'button_primary' => 'project-button project-button-primary',
'card' => 'project-card',
],
],
'ui' => [
'preset' => 'utility',
'classes' => [
'field_input' => 'project-field project-field-input',
'button_primary' => 'project-button project-button-primary',
],
],
'ui' => [
'form_layout' => [
'mode' => 'responsive', // responsive | stacked
'full_width_fields' => ['title', 'description', 'canonical', 'extra'],
'compact_fields' => ['robots', 'schema_type', 'sitemap_
'ui' => [
'inventory' => [
'models' => [
'enabled' => true,
'default_limit' => 50,
'max_limit' => 200,
],
],
],
'models' => [
App\Models\Product::class => [
'route' => 'products.show',
'route_key' => 'slug',
'title' => 'title',
'description' => 'description',
'ui' => [
'enabled' => true,
'label' => 'Products',
'source' => fn () => App\Models\Product::query()->latest()->limit(50)->get(),
'key' => 'id',
'display' => ['title', 'name', 'slug'],
],
],
],
[
'sitemap' => [
' 'change_frequency' => 'weekly',
],
]
'models' => [
App\Models\Product::class => [
'route' => 'products.show',
'route_key' => 'slug',
'title' => 'title',
'description' => 'description',
'sitemap' => [
'enabled' => true,
'
'url_generation' => [
'base_url' => 'https://example.com',
'locale_strategy' => null, // inherits localization.url_strategy
],
'models' => [
App\Models\Product::class => [
'title' => 'title',
'description' => 'description',
'url' => [
'strategy' => 'pattern',
'pattern' => '/{locale}/products/{slug}',
'fields' => [
'slug' => 'slug',
],
],
'sitemap' => [
'enabled' => true,
'
'routes' => [
'about' => [
'title' => 'About',
'sitemap' => true,
'url' => [
'strategy' => 'pattern',
'pattern' => '/{locale}/about',
],
],
],
'features' => [
'commerce' => true,
],
'commerce' => [
'enabled' => true,
'offer' => [
'enabled' => 'auto',
'
'models' => [
App\Models\Product::class => [
'route' => 'products.show',
'route_key' => 'slug',
'type' => 'Product',
'commerce' => [
'enabled' => true,
'name' => 'name',
'description' => ['short_description', 'description'],
'image' => 'image_url',
'price' => 'price',
'currency' => 'currency',
'sku' => 'sku',
'brand' => 'brand.name',
'availability' => 'stock_status',
'condition' => 'condition',
],
],
],
'commerce' => [
'enabled' => true,
'name' => ['translations[locale={locale}].title', 'title'],
'description' => ['translations[locale={locale}].description', 'description'],
'brand' => 'brand.name',
],
'commerce' => [
'enabled' => true,
'name' => ['translations[locale={locale}].title', 'title'],
'price' => ['translations[locale={locale}].price', 'activeOffer.price', 'discount.price'],
'currency' => 'literal:IRR',
],
'price' => [
'relation' => 'translations',
'where' => ['locale' => '{locale}'],
'value' => 'price',
],
use Zarbin\Seo\Contracts\CommerceSeo;
use Zarbin\Seo\Data\CommerceData;
class Product extends Model implements Seoable, CommerceSeo
{
use HasSeo;
public function toCommerceData(?string $locale = null): CommerceData|array|null
{
return CommerceData::make([
'name' => $this->name,
'price' => $this->price,
'currency' => 'IRR',
'availability' => $this->stock > 0 ? 'in_stock' : 'out_of_stock',
'brand' => $this->brand?->name,
'sku' => $this->sku,
]);
}
}
seo()
->title('Product title')
->commerce([
'price' => 120000,
'currency' => 'IRR',
'availability' => 'in_stock',
])
->render();
bash
php artisan vendor:publish --tag=zarbin-seo-config
bash
php artisan vendor:publish --tag=zarbin-seo-migrations
php artisan vendor:publish --tag=zarbin-seo-views
php artisan vendor:publish --tag=zarbin-seo-translations
bash
php artisan vendor:publish --tag=zarbin-seo-config
bash
php artisan vendor:publish --tag=zarbin-seo-migrations
php artisan migrate
bash
php artisan zarbin-seo:sitemap
php artisan zarbin-seo:sitemap --locale=fa
php artisan zarbin-seo:sitemap --index
php artisan zarbin-seo:sitemap --output=public/sitemap.xml
php artisan zarbin-seo:sitemap --count
php artisan zarbin-seo:robots
php artisan zarbin-seo:robots --output=public/robots.txt
bash
php artisan vendor:publish --tag=zarbin-seo-migrations
php artisan migrate
bash
php artisan vendor:publish --tag=zarbin-seo-translations
bash
php artisan vendor:publish --tag=zarbin-seo-views
blade
<form method="POST" action="{{ route('admin.products.update', $product) }}">
@csrf
@method('PUT')
<!-- Product fields... -->
<x-zarbin-seo::model-fields :source="$product" locale="fa" name="seo" />
<button type="submit">Save</button>
</form>
bash
php artisan vendor:publish --tag=zarbin-seo-views
bash
php artisan zarbin-seo:doctor
bash
php artisan vendor:publish --tag=zarbin-seo-views --force
bash
php artisan zarbin-seo:install
php artisan zarbin-seo:install --all
php artisan zarbin-seo:install --migrations --run-migrations
bash
php artisan zarbin-seo:doctor
php artisan zarbin-seo:doctor --strict
php artisan zarbin-seo:doctor --json
bash
php artisan zarbin-seo:doctor
php artisan zarbin-seo:doctor --strict
php artisan zarbin-seo:doctor --json
bash
php artisan zarbin-seo:check
php artisan zarbin-seo:check --route=home
php artisan zarbin-seo:check --route=home --locale=fa --render
php artisan zarbin-seo:check --model="App\Models\Post" --id=1 --json
bash
php artisan zarbin-seo:sitemap --output=public/sitemap.xml
php artisan zarbin-seo:robots --output=public/robots.txt
bash
php scripts/e2e-consumer-app.php