blogsforseo

Connect: Laravel

Connect a Laravel site: a config entry, a small helper, four routes and two Blade views.

This is the simple way: we send finished HTML with the SEO done, and your site shows it in its own design. Using a coding agent? Copy the prompt from your dashboard's Integration page instead: it has your values filled in and the same steps for this framework.

How it works

  • The key comes from the environment. BLOGSFORSEO_API_KEY is read on the server and sent as Authorization: Bearer <key>. It's never in the code, the templates or the browser.
  • Every visit asks our API. The pages are rendered on each request, never saved at build time, so new posts, edits and removals show up on their own. The one rule for caching: nothing may keep our API's answer for more than 5 minutes. If you add a cache, keep it at 300 seconds or less, and check your CDN rules on /blog and /blog-sitemap.xml too.
  • A 404 from us is a 404 from you. An unknown or removed post must answer a real 404, not an empty page. Any other error fails the request, so your framework shows its normal error page.

Match the routes to your post URL pattern in the dashboard: these use /blog and /blog/{slug}. In this guide, {API} means https://blogsforseo.com/api/v1.

Set it up

Uses Laravel's HTTP client (Laravel 9 or newer). Read the key through config(), not env(): after php artisan config:cache, env() returns null.

// config/services.php: add this entry to the returned array
'blogsforseo' => [
    'url' => rtrim(env('BLOGSFORSEO_API_URL', 'https://blogsforseo.com'), '/').'/api/v1',
    'key' => env('BLOGSFORSEO_API_KEY'),
],
<?php
// app/Support/BlogsForSeo.php

namespace App\Support;

use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;

class BlogsForSeo
{
    /** Asks the Content API on every request. A 404 from the API is this page's 404. */
    public static function get(string $path, array $query = []): Response
    {
        $response = Http::withToken(config('services.blogsforseo.key'))
            ->timeout(10)
            ->get(config('services.blogsforseo.url').$path, $query);

        abort_if($response->notFound(), 404);

        return $response->throw();
    }
}
// routes/web.php: add these routes, and add these two use lines to the top of the file
// if they're not there yet (Laravel's web.php already imports Route).
use App\Support\BlogsForSeo;
use Illuminate\Http\Request;

Route::get('/blog', function (Request $request) {
    $page = max(1, $request->integer('page', 1));

    return view('blog.index', BlogsForSeo::get('/html/posts', ['page' => $page])->json());
});

Route::get('/blog/{slug}', function (string $slug) {
    return view('blog.post', BlogsForSeo::get('/html/posts/'.rawurlencode($slug))->json());
})->where('slug', '[a-z0-9-]+');

Route::get('/blog-sitemap.xml', function () {
    return response(BlogsForSeo::get('/sitemap', ['format' => 'xml'])->body())
        ->header('Content-Type', 'application/xml');
});

Route::get('/llms.txt', function () {
    return response(BlogsForSeo::get('/llms.txt')->body())
        ->header('Content-Type', 'text/plain; charset=UTF-8');
});

Views, in your own layout. head and body are already escaped by us, so print them raw:

{{-- resources/views/blog/post.blade.php; blog/index.blade.php is the same without the head section --}}
@extends('layouts.app')

@section('head')
    {!! $head !!}
@endsection

@section('content')
    {!! $body !!}
@endsection

Your layout needs @yield('head') inside <head>; leave out your own <title> on post pages.

  • IndexNow key file: save public/<your IndexNow key>.txt containing just the key.
  • robots.txt: add the sitemap line from your dashboard (Sitemap: https://<your domain>/blog-sitemap.xml) to public/robots.txt.
  • Caching: these routes fetch on every request. If you wrap a call in Cache::remember, use 300 seconds or less, and don't put the blog routes behind a response cache that keeps pages longer.

The advanced way

Fetch /posts and /posts/{slug} instead of the /html endpoints, and build the page from the fields in the Content API reference, following the advanced guide. bodyHtml is already sanitised: print it raw. When you put jsonLd in a <script type="application/ld+json">, escape <:

json_encode($post['jsonLd'], JSON_HEX_TAG | JSON_UNESCAPED_SLASHES)

Check it

Run these against your site (local or live). -A GPTBot asks the way an AI crawler does, without JavaScript:

curl -A GPTBot https://<your domain>/blog                 # lists posts, or "No posts yet"
curl -A GPTBot https://<your domain>/blog/<a post slug>   # <h1>, article text, canonical link, application/ld+json
curl -i https://<your domain>/blog/not-a-real-post        # HTTP 404
curl https://<your domain>/blog-sitemap.xml               # the same posts as GET {API}/sitemap
curl https://<your domain>/llms.txt                       # plain text
curl https://<your domain>/<your IndexNow key>.txt        # just the key
curl https://<your domain>/robots.txt                     # has the Sitemap: line

Then check it stays fresh: the number of posts on /blog matches total from the API. Finish with the simple guide's dashboard steps: check the post address, run the health check and connect Search Console.