Search code examples
phpsymfonysymfony-routing

Migrating legacy project to Symfony routing issues


I'm trying to start migrating my legacy project to Symfony, piece by piece (Strangler Application). I followed this documentation, but I can only load my home URL / of my legacy application. Other URL's will give me a 404 even before I get to the LegacyBridge class.

What do I miss?

EDIT:
Probably people don't understand my question. I have no issues starting my Symfony project. I'm trying to build a Strangler Application, slowly migrating my legacy code to Symfony. So my legacy code and Symfony application are running side by side. But everything is loaded through the frontend controller of Symfony.

So I modified the index.php from Symfony public/index.php.

I added a LegacyBridge class to the frontend controller of Symfony:

<?php
// public/index.php
use App\Kernel;
use App\LegacyBridge;
use Symfony\Component\Debug\Debug;
use Symfony\Component\HttpFoundation\Request;

require dirname(__DIR__).'/config/bootstrap.php';

/*
 * The kernel will always be available globally, allowing you to
 * access it from your existing application and through it the
 * service container. This allows for introducing new features in
 * the existing application.
 */
global $kernel;

if ($_SERVER['APP_DEBUG']) {
    umask(0000);

    Debug::enable();
}

if ($trustedProxies = $_SERVER['TRUSTED_PROXIES'] ?? $_ENV['TRUSTED_PROXIES'] ?? false) {
    Request::setTrustedProxies(explode(',', $trustedProxies), Request::HEADER_X_FORWARDED_ALL ^ Request::HEADER_X_FORWARDED_HOST);
}

if ($trustedHosts = $_SERVER['TRUSTED_HOSTS'] ?? $_ENV['TRUSTED_HOSTS'] ?? false) {
    Request::setTrustedHosts([$trustedHosts]);
}

$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);

/*
 * LegacyBridge will take care of figuring out whether to boot up the
 * existing application or to send the Symfony response back to the client.
 */
$scriptFile = LegacyBridge::prepareLegacyScript($request, $response, __DIR__);
if ($scriptFile !== null) {
    require $scriptFile;
} else {
    $response->send();
}
$kernel->terminate($request, $response);

I already get a 404 on line $response = $kernel->handle($request);.

<?php
//src/LegacyBridge.php

namespace App;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class LegacyBridge
{
    public static function prepareLegacyScript(Request $request, Response $response, string $publicDirectory)
    {

        // If Symfony successfully handled the route, you do not have to do anything.
        if (false === $response->isNotFound()) {
            return;
        }

        // Figure out how to map to the needed script file
        // from the existing application and possibly (re-)set
        // some env vars.

        $dir = $request->server->get('SERVER_NAME');
        $dir = str_replace('.test', '.nl', $dir);

        $legacyScriptFilename = realpath($publicDirectory . '/../../' . $dir . '/index.php');

        return $legacyScriptFilename;
    }
}

Solution

  • The 404 response should be generated on the line you mention (as expected) but not sent until the else block below:

    $scriptFile = LegacyBridge::prepareLegacyScript($request, $response, __DIR__);
    if ($scriptFile !== null) {
        require $scriptFile;
    } else {
        $response->send();
    }
    

    Which means $scriptFile is null. So, in your LegacyBridge add the following to debug the path:

    $legacyScriptFilename = realpath($publicDirectory . '/../../' . $dir . '/index.php');
    
    // next two lines for debug
    var_dump($legacyScriptFilename);
    die();
    
    return $legacyScriptFilename;
    

    That will dump $legacyScriptFilename and kill further execution, so you can verify the if the path is correct.

    If you are getting an uncaught Exception try catching and suppressing it like so:

    try {
      $response = $kernel->handle($request);
    } catch (\Exception $e) {
      $response = new Response(); 
      $response->setStatusCode(404);
    }
    

    That should allow execution to continue despite the Exception.