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.


Looking for PHP, Laminas or Mezzio Support?

As part of the Laminas Commercial Vendor Program, Apidemia offers expert technical support and services for:

  • Migration from Laminas MVC to Mezzio or Dotkernel Headless Platform.
  • Migration from legacy Laminas API Tools (formerly Apigility) to Dotkernel API
  • Mezzio and Laminas Consulting and Technical Audit
  • Modernising Legacy Applications
  • Leave a Reply

    Your email address will not be published. Required fields are marked *

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>