简体   繁体   English

将旧项目迁移到 Symfony 路由问题

[英]Migrating legacy project to Symfony routing issues

I'm trying to start migrating my legacy project to Symfony, piece by piece (Strangler Application).我正在尝试开始将我的旧项目逐步迁移到 Symfony(Strangler 应用程序)。 I followed this documentation , but I can only load my home URL / of my legacy application.我遵循了这个文档,但我只能加载我的旧应用程序的 URL /我的家。 Other URL's will give me a 404 even before I get to the LegacyBridge class.即使在我到达LegacyBridge class 之前,其他 URL 也会给我一个 404。

What do I miss?我想念什么?

EDIT:编辑:
Probably people don't understand my question.可能人们不明白我的问题。 I have no issues starting my Symfony project.我开始我的 Symfony 项目没有问题。 I'm trying to build a Strangler Application, slowly migrating my legacy code to Symfony.我正在尝试构建一个 Strangler 应用程序,将我的旧代码慢慢迁移到 Symfony。 So my legacy code and Symfony application are running side by side.所以我的遗留代码和 Symfony 应用程序并排运行。 But everything is loaded through the frontend controller of Symfony.但是一切都是通过 Symfony 的前端 controller 加载的。

So I modified the index.php from Symfony public/index.php .所以我从 Symfony public/index.php

I added a LegacyBridge class to the frontend controller of Symfony:我在 Symfony 的前端 controller 中添加了 LegacyBridge class:

<?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);我已经在$response = $kernel->handle($request);线上得到了 404 . .

<?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;
    }
}

The 404 response should be generated on the line you mention (as expected) but not sent until the else block below: 404响应应该在您提到的行上生成(如预期的那样),但直到下面的else块才发送:

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

Which means $scriptFile is null.这意味着$scriptFile是 null。 So, in your LegacyBridge add the following to debug the path:因此,在您的 LegacyBridge 中添加以下内容以调试路径:

$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.这将转储$legacyScriptFilename并终止进一步的执行,因此您可以验证路径是否正确。

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.尽管出现异常,这应该允许继续执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM