SEO in Laravel Without a Package: Meta Tags, JSON-LD and a Sitemap

2026-12-04来源 dev.toEnglish

Most Laravel SEO tutorials start with "install this package". For a lot of business sites you don't need one. A small value object, one Blade component and a sitemap route cover 90% of what search engines actually read.

This is the setup we use on client sites at RyxaDev, simplified so you can drop it into any Laravel 11+ app.

1. A tiny Seo value object

Every page needs the same handful of fields: title, description, canonical URL, image, robots and some structured data. Put them in one class instead of scattering @section('title') calls across views.

namespace App\Support\Seo; class Seo { public array $schema = []; public string $robots = 'index, follow, max-image-preview:large'; public function __construct( public ?string $title = null, public ?string $description = null, public ?string $image = null, public ?string $canonical = null, ) {} public static function make(?string $title = null, ?string $description = null): self { return new self($title, $description); } public function noindex(): self { $this->robots = 'noindex, follow'; return $this; } public function schema(array $node): self { $this->schema[] = $node; return $this; } } 

Controllers build it and pass it to the view:

public function show(Post $post): View { $seo = Seo::make($post->meta_title ?? $post->title, $post->excerpt) ->schema(SchemaFactory::article($post)); return view('blog.show', compact('post', 'seo')); } 

2. One Blade component for the <head>

@props(['seo']) <title>{{ $seo->title }} | {{ config('app.name') }}</title> <meta name="description" content="{{ $seo->description }}"> <meta name="robots" content="{{ $seo->robots }}"> <link rel="canonical" href="{{ $seo->canonical ?? url()->current() }}"> <meta property="og:title" content="{{ $seo->title }}"> <meta property="og:description" content="{{ $seo->description }}"> <meta property="og:url" content="{{ $seo->canonical ?? url()->current() }}"> @if ($seo->image) <meta property="og:image" content="{{ $seo->image }}"> @endif @foreach ($seo->schema as $node) <script type="application/ld+json">{!! json_encode($node, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) !!}</script> @endforeach 

Two details that are easy to get wrong:

  • Always output a canonical. Query strings (?utm_source=..., ?page=1) otherwise create duplicate URLs.
  • Escape everything except the JSON-LD, and build that JSON with json_encode, never by hand in Blade.

3. JSON-LD from a factory, not from views

Keep structured data in PHP where you can test it:

class SchemaFactory { public static function organization(): array { return [ '@context' => 'https://schema.org', '@type' => 'Organization', '@id' => url('/#organization'), 'name' => config('app.name'), 'url' => url('/'), 'logo' => asset('images/logo.png'), ]; } public static function article(Post $post): array { return [ '@context' => 'https://schema.org', '@type' => 'BlogPosting', 'headline' => $post->title, 'datePublished' => $post->published_at?->toAtomString(), 'dateModified' => $post->updated_at?->toAtomString(), 'author' => ['@type' => 'Organization', '@id' => url('/#organization')], 'mainEntityOfPage' => route('blog.show', $post), ]; } } 

A feature test can now assert that a blog page contains a BlogPosting node, which is far more reliable than eyeballing the Rich Results Test after every deploy.

4. A sitemap route

Route::get('sitemap.xml', function () { $posts = Post::query()->published()->latest('updated_at')->get(['slug', 'updated_at']); return response() ->view('seo.sitemap', compact('posts')) ->header('Content-Type', 'application/xml'); }); 
<?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url><loc>{{ url('/') }}</loc></url> @foreach ($posts as $post) <url> <loc>{{ route('blog.show', $post) }}</loc> <lastmod>{{ $post->updated_at->toAtomString() }}</lastmod> </url> @endforeach </urlset> 

Reference it from robots.txt and submit it once in Google Search Console.

5. What this does not cover

Meta tags are the easy part. Rankings usually move because of the boring work: fast pages, clean internal linking, no orphan pages, and content that answers the query. We keep a longer technical SEO checklist for that side of things.

Wrap-up

  • One Seo object per request
  • One Blade component that renders it
  • Structured data built in PHP and covered by tests
  • A sitemap generated from the database

No package, around 150 lines of code, and you fully control the output.

Written by the team at RyxaDev, a web development and SEO agency building Laravel, React and Next.js sites.

原文:https://www.a8q9.cn/en/article/seo-in-laravel-without-a-package-meta-tags-json-ld-and-a-sitemap-6f3aac.html

相关文章