[英]Migrating legacy project to Symfony routing issues
我正在嘗試開始將我的舊項目逐步遷移到 Symfony(Strangler 應用程序)。 我遵循了這個文檔,但我只能加載我的舊應用程序的 URL /
我的家。 即使在我到達LegacyBridge
class 之前,其他 URL 也會給我一個 404。
我想念什么?
編輯:
可能人們不明白我的問題。 我開始我的 Symfony 項目沒有問題。 我正在嘗試構建一個 Strangler 應用程序,將我的舊代碼慢慢遷移到 Symfony。 所以我的遺留代碼和 Symfony 應用程序並排運行。 但是一切都是通過 Symfony 的前端 controller 加載的。
所以我從 Symfony public/index.php
。
我在 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);
我已經在$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;
}
}
404響應應該在您提到的行上生成(如預期的那樣),但直到下面的else
塊才發送:
$scriptFile = LegacyBridge::prepareLegacyScript($request, $response, __DIR__);
if ($scriptFile !== null) {
require $scriptFile;
} else {
$response->send();
}
這意味着$scriptFile
是 null。 因此,在您的 LegacyBridge 中添加以下內容以調試路徑:
$legacyScriptFilename = realpath($publicDirectory . '/../../' . $dir . '/index.php');
// next two lines for debug
var_dump($legacyScriptFilename);
die();
return $legacyScriptFilename;
這將轉儲$legacyScriptFilename
並終止進一步的執行,因此您可以驗證路徑是否正確。
如果您遇到未捕獲的異常,請嘗試像這樣捕獲並抑制它:
try {
$response = $kernel->handle($request);
} catch (\Exception $e) {
$response = new Response();
$response->setStatusCode(404);
}
盡管出現異常,這應該允許繼續執行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.