Using both Laravel and non-laravel-PHP

Tags: , ,



I am trying to make Laravel work with a non-laravel script.

I need Laravel to handle only routes that are set in the web.php file, and allow anything else that’s not defined to be handled by my non-laravel-PHP’s legacy_index.php.

enter image description here

This is the folder setup that I have in my public. legacy_index.php is the one that belongs to my non-laravel-php script. The legacy_index.php script has a URL parsing algorithm in it. I want Laravel to allow me to use the legacy_index.php if the path I am trying to access is not declared in the routes, but without showing the legacy_index.php file in the link.

As an example: Laravel would process these routes:

  • 127.0.0.1:8000/api/getUserData
  • 127.0.0.1:8000/api/getAppointments

But would ignore these routes:

  • 127.0.0.1:8000/mail
  • 127.0.0.1:8000/user-profile

And would let my legacy_index.php to handle the routing for these two.

I have tried by editing the .htaccess file, trying to redirect to these links from the routes, but nothing worked.

Did anyone do this before and has any tips/pointers? I am developing on Windows, using Laravel 8.42.

Answer

It looks like your routes are neatly identified by the a simple rule that if they begin with “/api” then you want Laravel’s index.php to handle it but if it doesn’t then you want legacy_index.php to handle it. Therefore adding another conditional rewrite rule to the .htaccess might be 1 method.

...

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/api/(.+)
RewriteRule ^ index.php [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index_legacy.php [L]


Source: stackoverflow