2

Implementation of SEO friendly URL in an generic Laminas Mezzio app

 10 months ago
source link: https://www.dotkernel.com/how-to/implementation-seo-friendly-url-laminas-mezzio/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Posted by MarioRadu on June 30, 2023 | No Comments

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.


Looking for PHP, Laminas or Mezzio Support?

As part of the Laminas Commercial Vendor Program , Apidemia offers the expert technical support and services for: Modernise Legacy Applications Migrations from any version of Zend Framework to Laminas Mezzio and Laminas Consulting and Technical Audits

Leave a Reply Cancel Reply

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

Comment

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>

Name *
Email *
Website

Save my name, email, and website in this browser for the next time I comment.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK