How we migrated our legacy PHP app to Laravel in a progressive manner

HenonoaH
PearlThoughts
Published in
2 min readMay 31, 2020

We have been tasked with migrating 7 years old big monolith MVC PHP app to Laravel. The legacy app was written in Yii 1.1 which is a MVC framework. The codebase was not well organized, not written with the highest quality of coding standards. The client wanted to stay the app with PHP ecosystem since their team is comfortable with PHP. We considered Yii2 but decided to go with Laravel because there’s a conflict of interest in maintaining the Yii2 codebase within the core authors itself.

How should we approach the migration?

  1. Set up Laravel as the dominant framework, add Yii app 3rd party app to Laravel.
  2. Set up Yii as a dominant framework, add Laravel as 3rd party app
  3. Set up both apps as independent on their own, have Apache to take care of routing and hack the sessions to share among both apps.

We decided to go with the last approach mentioned above because it required less code & configuration change, allows the legacy app to function as normal, and provides a space for Laravel to develop new features or rewrite parts of them.

Directory structure:

We isolated both apps in a separate repo and they should be under the same root directory.

/srv/legacy-yii-app

/srv/laravel-app

Apache Virtual Host to manage routes:

<VirtualHost *:80>
ServerName legacy-app.local
DocumentRoot /srv

RewriteEngine on
RewriteRule ^/user/index$ /laravel/admin/auth/user [P]
RewriteCond %{REQUEST_URI} !^/laravel
RewriteRule !^legacy-yii-app/ /legacy-yii-app/backend/web%{REQUEST_URI} [L,NC]
RewriteCond %{REQUEST_URI} !^/legacy-yii-app
RewriteRule ^/laravel(.*)$ /laravel-app/public/$1 [L,NC]

<Directory "/srv">
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Add .htaccess file in application webroot if not already exists:

Add these lines to .htaccess file

  1. Yii -RewriteBase /
  2. Laravel -RewriteBase /laravel

Managing authentication:

app/Guards/LegacySessionGuard.php

Session values are available in $_SESSION. Hence, we are creating a custom session guard in Laravel to read session details you can also secure session data via secret signing technique

app/Http/Middleware/Authenticate.php

Add this code snippet to authenticate the legacy user in the Larvael app.

app/Providers/AuthServiceProvider.php

Register legacy session guard in the auth service provider.

config/auth.php

Change default guard to legacy

'defaults' => [
'guard' => 'legacy',
'passwords' => 'users'
]

Add the legacy guard in guards list

'guards' => [
'legacy' => [
'driver' => 'legacy',
'provider' => 'users'
]
]

Reference: https://stackoverflow.com/a/43089578/9715025

--

--