Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

FazBeni24's avatar

Strangler Pattern updating legacy site into Laravel

I am working on modernizing a bespoke PHP website, with its files located within a "legacy-folder". The current setup authenticates users, assigning them a unique session ID to access personalized data. For example, accessing [login to view URL] displays sanction details specific to the authenticated user.

My goal is to incrementally migrate this website to Laravel, starting with the sanctions page. The new Laravel version of the page should automatically authenticate users, replacing the legacy PHP page without requiring a complete site rebuild. I have initiated this process by setting up a Laravel 8 installation in a "laravel-folder" due to the PHP version constraints of the legacy site.

The plan is to detect when a user navigates to the legacy sanctions page, check if a corresponding Laravel route/page exists, automatically log the user in, and then serve the Laravel page instead.

Key Questions: URL Routing: What are the best practices or recommended approaches for redirecting requests from a legacy PHP page to a new Laravel route, ensuring seamless user authentication in the process?

Authentication Integration: How can I efficiently integrate the existing user session from the legacy system into Laravel, to avoid requiring users to log in again when redirected to the Laravel page?

Incremental Migration: Are there specific strategies or tools recommended for gradually migrating a legacy PHP site to Laravel, ensuring minimal disruption to the current user experience?

Additional Context:

My local development environment is set up with Laragon. The aim is to perform a gradual migration to Laravel without undertaking a full site rebuild at once.

Any advice, resources, or examples that could assist in this migration process would be greatly appreciated. I've already created a Laravel route intended to serve as a replacement for the legacy sanctions page.

Is there a way to load the legacy page from the public laravel folder?

Thanks.

0 likes
1 reply
martinbean's avatar
Level 80

@fazbeni24 The way I’ve migrated legacy projects to Laravel in the past, where the legacy site has just been PHP scripts, is almost the opposite of what you’ve done. I rename public/index.php to public/laravel-index.php, and then drop the legacy project’s files in the public directory. You can then update your web server’s configuration to either serve the file as is if it exists, or past the request to the Laravel front controller if it isn’t handled by a standalone file.

This way, you can slowly convert the legacy scripts to Laravel controllers and views. You pick a page, create the controller and view, and then delete the legacy script. Following this process, the contents of your public directory will gradually reduce in the number of the legacy project’s scripts, and the number of Laravel controllers and views will increase. Once you have re-factored everything, you can rename your public/laravel-index.php file back to public/index.php and you have yourself a modern Laravel project.

In terms of authentication, you could create a custom auth guard and provider that does the same as the current implementation.

Following the above means you don’t need to “detect” if the user is on a legacy or “new” page; they’ll get the legacy page if it exists or the Laravel-served page if that exists. You don’t need to do any detection or redirecting anywhere. You can, of course, set up redirects in your *.htaccess file or nginx configuration file to redirect legacy URLs (such as /view-sanction.php) to the equivalent Laravel-hosted URI once it’s available (i.e. /sanctions/{sanction}).

3 likes

Please or to participate in this conversation.