Implementation of SEO friendly URL in an generic Laminas Mezzio app

Prerequisites:

In the vast digital landscape of the internet, where websites compete for attention, having a well-crafted URL can make a significant difference. By incorporating human-readable slugs into website URLs, we can enhance user experience, improve search engine optimization (SEO), and foster better engagement. In this article, we discuss the importance, benefits and how to implement human-readable URLs.

The first 3 reasons why we should consider implementing slugs:

  • Readability and User-Friendly Experience
  • Search Engine Optimization (SEO)
  • Shareability and Trustworthiness

How to implement slugs into Dotkernel, a practical example:

Let's consider we want to implement a feature that gives users the possibility to view other user's profile.

When viewing another user's profile we have this URL: website.com/user/11 and this doesn't look so appealing, am I right?

So, the solution is to format the URL in a human-readable way, like this : website.com/user/john-doe.

To implement slugs into our codebase we are going to use a popular package, gedmo/doctrine-extensions.

First, we need to install the package via composer by running :

composer require gedmo/doctrine-extensions

After installing, we need to register the package's event listener into doctrine.

To do this, add the following codeblock in your doctrine global configuration. In our case it would be in the doctrine.global.php file.

'doctrine' => [
    'event_manager' => [
        'orm_default' => [
            'subscribers' => [
                Gedmo\Sluggable\SluggableListener::class,
            ]
        ]
    ],
]

This is all the configuration you will need, the next step is to create a new database column that will be our slug, we will add the column in the src/User/src/Entity/User.php entity and run the migrations.

use Gedmo\Mapping\Annotation as Gedmo;

...

/**
 * @ORM\Column(name="identity", type="string", length=64, nullable=false, unique=true)
 */
protected string $identity;

/**
 * @ORM\Column(name="slug", type="string", length=64, nullable=false, unique=true)
 * @Gedmo\Slug(fields={"identity"})
 */
protected string $slug;

When generating the slug we can specify what field(s) to be used, in our case the slug will be generated using the 'identity' field, more details here.

Now you can create a new user and see that the slug column was autocompleted.

To wrap things up, human-readable URL's are a must nowadays, thanks to its swift implementation and multiple benefits.

Frequently Asked Questions

What are the benefits of human-readable slugs cited in this article? +

Readability and a more user-friendly experience, better search engine optimization (SEO), and improved shareability and trustworthiness of the URL.

What package is used to generate slugs in this example? +

gedmo/doctrine-extensions, installed by running composer require gedmo/doctrine-extensions.

How is the slug listener registered with Doctrine? +

By adding Gedmo\Sluggable\SluggableListener::class under the doctrine.event_manager.orm_default.subscribers configuration, e.g. in doctrine.global.php.

How is the slug field configured on the entity in this example? +

A new slug column is added (e.g. to src/User/src/Entity/User.php) annotated with @Gedmo\Slug(fields={"identity"}), so the slug is generated automatically from the identity field, for example when a new user is created.