Laravel 10 - SEO Analyzer

Touseef Afridi
12 Sep 24

Laravel 10 - SEO Analyzer

In this tutorial, we will discuss how to retrieve SEO data from a remote site using the SEO Analyzer package in Laravel 10, which helps optimize websites for better search rankings.


If you're a video person, feel free to skip the post and check out the video instead!


Quick Overview

This guide walks you through the process of adding an SEO analysis feature to your Laravel application using the madeitbelgium/seo-analyzer package. You'll begin by setting up a new Laravel project and preparing the development environment. After installing the SEO Analyzer package, you'll create a controller to handle the analysis, define a route to connect it to the browser, and finally test the setup to display SEO metrics for any website. Along the way, you’ll organize your project structure efficiently, ensuring your application is ready for scaling and real-world SEO tasks. With each step, you’ll also gain insight into how SEO data can be extracted and displayed in a user-friendly manner. By the end, you'll have a working tool that can scan and reveal key SEO information directly from your Laravel app.

Step # 1 : Create a New Laravel Project.

Let’s start by setting up a fresh Laravel project. You can use either the Laravel installer or Composer, whichever you prefer. If you have Laravel installed globally, run.
laravel new seo-analyzer
Or, if you’re using Composer, run.
composer create-project laravel/laravel --prefer-dist seo-analyzer
This will create a new Laravel project named seo-analyzer, which will serve as the foundation for everything we're about to build. Composer will handle the installation and set up all the required files and directories for your Laravel application. At this point, you can proceed to the next steps, confident that your development environment is properly configured.

Step # 2 : Open and Prepare the Project.

Start by launching your terminal (for example, Git Bash) and navigate to your project’s root directory. You can do this by running the following command.
cd c:xampp/htdocs/seo-analyzer
Once you’re inside the project folder, you need to install the necessary frontend dependencies required by Laravel Vite. These dependencies will ensure that your development environment is set up correctly to handle the frontend assets like JavaScript and CSS. To install them, run the following command.
npm install && npm run dev
Keep this terminal open for Vite, and open a new terminal tab or window in the same project directory to run Laravel commands, like generating controllers or setting up routes, without interrupting the Vite server.

Step # 3 : Install Install the SEO Analyzer Package.

To enable our application to perform SEO analysis, we need to install the madeitbelgium/seo-analyzer package. Open your terminal and run the following command.
composer require madeitbelgium/seo-analyzer
This package provides a simple way to automatically scan any given website and retrieve important SEO metrics such as page titles, meta descriptions, keywords, headings, broken links, and much more. After running the command, Composer will download and register the package into your Laravel project, making its powerful SEO analysis features ready for use through a convenient facade. With this setup, we can easily integrate SEO checks directly into our application's logic without needing to manually write crawlers or analyzers from scratch. This allows us to focus on using the data generated from the analysis rather than worrying about the technicalities of extracting it.

Step # 4 : Create the SEO Controller.

With the package installed, the next step is to create a controller that will handle the SEO analysis logic. In your terminal, run the following command to generate a new controller named SeoController.
php artisan make:controller SeoController
Once the controller is created, open the newly generated SeoController.php file and replace its content with the following code. This controller will be responsible for triggering the SEO analysis process.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use MadeITBelgium\SeoAnalyzer\SeoFacade as SEO;
class SeoController extends Controller
{
    public function index()
    {
        // Pass the website URL or relevant domain to the SEO::analyze() method for analysis.
        $result = SEO::analyze('https://www.lipsum.com/');
        // Output the analysis result.
        dd($result);
    }
}
In this controller, we are using the SeoFacade provided by the package to perform an SEO scan on a sample URL (https://www.lipsum.com/). When the index() method is called, it will initiate the analysis process and immediately output (dump) the results onto the screen for easy inspection. This helps ensure that the package is working properly by providing an immediate view of the SEO metrics it extracts. It’s a simple yet effective way to verify that the integration was successful and that the data displayed aligns with expectations.

Step # 5 : Define the Route.

Now that the controller is ready, we need to set up a route that points to it. Open your routes/web.php file and add the following lines.
use App\Http\Controllers\SeoController;
Route::get('/', [SeoController::class, 'index']);
Here, we’re first importing the SeoController class and then defining a route that listens for a GET request at the root URL (/). When someone visits the homepage of our application, it will automatically trigger the index() method of the SeoController, perform the SEO analysis, and display the results. This step connects the web interface with the logic we just built, making the feature accessible via a browser.

Step # 6 : Run and Test the Application.

With everything set up, it’s time to test if everything works as expected. Start the Laravel development server by running.
php artisan serve
Now open your browser and visit: http://127.0.0.1:8000. You should see the SEO analysis details for the sample website.

When you access the page, the SeoController will immediately kick in, trigger the SEO analysis for the predefined sample website (https://www.lipsum.com/), and display a detailed breakdown of SEO metrics directly in the browser. The data will include information like page title, meta description, keyword usage, and other important SEO factors. If you see the analysis data dumped on the screen, it means the integration was successful and you’re ready to move forward with further customizations or enhancements.

Conclusion

By following this guide, you’ve successfully integrated an SEO analysis feature into your Laravel application using the madeitbelgium/seo-analyzer package. With this setup, you can now scan any website and retrieve important SEO metrics directly from your app. This foundation not only simplifies the process of analyzing SEO performance but also opens up possibilities for expanding the functionality as needed, such as adding user-submitted URLs, automating reports, or integrating with other tools for deeper insights.
For more advanced usage and customization options, be sure to check out the official documentation of the SEO Analyzer package.
Share this with friends!


"Give this post some love and slap that 💖 button as if it owes you money! 💸😄"
0

0 Comments

To engage in commentary, kindly proceed by logging in or registering