将遗留项目迁移到 Symfony 路由问题
Migrating legacy project to Symfony routing issues
我正在尝试开始将我的遗留项目一点一点地迁移到 Symfony(Strangler 应用程序)。我关注了 this documentation,但我只能加载我的遗留应用程序的主页 URL /
。其他 URL 甚至在我到达 LegacyBridge
class.
之前就会给我一个 404
我错过了什么?
编辑:
可能人们不明白我的问题。我在开始我的 Symfony 项目时没有遇到任何问题。我正在尝试构建一个 Strangler 应用程序,慢慢地将我的遗留代码迁移到 Symfony。所以我的遗留代码和 Symfony 应用程序 运行 并排。但是一切都是通过 Symfony 的前端控制器加载的。
所以我从 Symfony public/index.php
修改了 index.php。
我在 Symfony 的前端控制器中添加了一个 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);
我已经在线收到 404 $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;
}
}
404 响应 应该 在您提到的行上生成(如预期的那样)但直到 else
才发送下面的块:
$scriptFile = LegacyBridge::prepareLegacyScript($request, $response, __DIR__);
if ($scriptFile !== null) {
require $scriptFile;
} else {
$response->send();
}
这意味着 $scriptFile
为空。因此,在您的 LegacyBridge 中添加以下内容以调试路径:
$legacyScriptFilename = realpath($publicDirectory . '/../../' . $dir . '/index.php');
// next two lines for debug
var_dump($legacyScriptFilename);
die();
return $legacyScriptFilename;
这将转储 $legacyScriptFilename
并终止进一步执行,因此您可以验证路径是否正确。
如果您遇到未捕获的 Exception 尝试捕获并抑制它,如下所示:
try {
$response = $kernel->handle($request);
} catch (\Exception $e) {
$response = new Response();
$response->setStatusCode(404);
}
尽管有异常,这应该允许执行继续。
我正在尝试开始将我的遗留项目一点一点地迁移到 Symfony(Strangler 应用程序)。我关注了 this documentation,但我只能加载我的遗留应用程序的主页 URL /
。其他 URL 甚至在我到达 LegacyBridge
class.
我错过了什么?
编辑:
可能人们不明白我的问题。我在开始我的 Symfony 项目时没有遇到任何问题。我正在尝试构建一个 Strangler 应用程序,慢慢地将我的遗留代码迁移到 Symfony。所以我的遗留代码和 Symfony 应用程序 运行 并排。但是一切都是通过 Symfony 的前端控制器加载的。
所以我从 Symfony public/index.php
修改了 index.php。
我在 Symfony 的前端控制器中添加了一个 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);
我已经在线收到 404 $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;
}
}
404 响应 应该 在您提到的行上生成(如预期的那样)但直到 else
才发送下面的块:
$scriptFile = LegacyBridge::prepareLegacyScript($request, $response, __DIR__);
if ($scriptFile !== null) {
require $scriptFile;
} else {
$response->send();
}
这意味着 $scriptFile
为空。因此,在您的 LegacyBridge 中添加以下内容以调试路径:
$legacyScriptFilename = realpath($publicDirectory . '/../../' . $dir . '/index.php');
// next two lines for debug
var_dump($legacyScriptFilename);
die();
return $legacyScriptFilename;
这将转储 $legacyScriptFilename
并终止进一步执行,因此您可以验证路径是否正确。
如果您遇到未捕获的 Exception 尝试捕获并抑制它,如下所示:
try {
$response = $kernel->handle($request);
} catch (\Exception $e) {
$response = new Response();
$response->setStatusCode(404);
}
尽管有异常,这应该允许执行继续。