Enhancing URL Handling with Tenant Domain Extraction

Introduction

Our application required a more efficient and accurate method for extracting tenant domains to handle URL redirection. The previous implementation had limitations in handling both subdomain-only and full domain formats.

The Challenge

The legacy system had issues with:

  • Incorrect subdomain extraction
  • Inconsistent URL construction
  • Potential errors in redirecting users to demos

Development

To address these challenges, we refactored the RandomDemoController to improve tenant domain extraction.

$tenantDomain = $randomTenant->domain;
$subdomain = str_contains($tenantDomain, '.')
    ? explode('.', $tenantDomain)[0]
    : $tenantDomain;

We utilized the tenantUrl function to ensure correct URL construction, passing the extracted subdomain as an argument.

Example

Before the refactor:

$demoUrl = tenantUrl($request, $randomTenant->domain, '/');

After the refactor:

$demoUrl = tenantUrl($request, $subdomain, '/');

Conclusion

This enhancement improved the accuracy of URL handling and demo redirection. The refactor also maintained adherence to project standards for code clarity and organization. The key takeaway is the importance of considering various domain formats when implementing URL handling mechanisms.


Generated with Devlog.ist

Gerardo Ruiz

Gerardo Ruiz

Author

Share: