{"id":1081,"date":"2025-06-12T12:22:50","date_gmt":"2025-06-12T04:22:50","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/php\/php-application-manual\/symfony\/1081.html"},"modified":"2025-06-12T12:22:50","modified_gmt":"2025-06-12T04:22:50","slug":"%e5%9c%a8symfony%e4%b8%ad%e5%a4%84%e7%90%86%e5%ad%90%e8%af%b7%e6%b1%82%ef%bc%9a%e5%ae%9e%e7%94%a8%e6%8c%87%e5%8d%97","status":"publish","type":"my1js","link":"https:\/\/www.zhaozhao123.cn\/php\/my1js\/1081.html","title":{"rendered":"\u5728Symfony\u4e2d\u5904\u7406\u5b50\u8bf7\u6c42\uff1a\u5b9e\u7528\u6307\u5357"},"content":{"rendered":"<div class=\"wp-block-columns p-0 border is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-columns px-4 py-3 border-bottom has-background is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\" style=\"background:linear-gradient(243deg,rgb(238,238,238) 0%,rgba(58,166,242,0.15) 100%)\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-6c531013 wp-block-group-is-layout-flex\">\r\n<figure class=\"wp-block-image size-thumbnail is-resized is-style-rounded is-style-rounded--1\"><img decoding=\"async\" src=\"https:\/\/www.zhaozhao123.cn\/myitems\/images\/sites16\/2025\/06\/dyA-1-400x300.jpg\" alt=\"Bug&#32534;&#35793;&#29422;\" class=\"wp-image-1842\" style=\"object-fit:cover;width:30px;height:30px\"><\/figure>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading my-0\" style=\"font-size:clamp(0.875rem, 0.875rem + ((1vw - 0.2rem) * 0.175), 1rem);\">Bug&#32534;&#35793;&#29422;<\/h2>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n\r\n\r\n\r\n<div class=\"wp-block-columns px-xl-5 px-4 py-xl-4 py-3 is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<p>&#26631;&#39064;&#65306;&#22312;Symfony&#20013;&#22788;&#29702;&#23376;&#35831;&#27714;&#65306;&#23454;&#29992;&#25351;&#21335;<\/p>\n<p>&#22312;&#29616;&#20195;Web&#24320;&#21457;&#20013;&#65292;&#23376;&#35831;&#27714;&#65288;&#20063;&#31216;&#20026;&#24322;&#27493;&#35831;&#27714;&#65289;&#21464;&#24471;&#36234;&#26469;&#36234;&#24120;&#35265;&#65292;&#29305;&#21035;&#26159;&#22312;&#38656;&#35201;&#23545;&#22810;&#20010;&#36164;&#28304;&#36827;&#34892;&#24182;&#21457;&#25805;&#20316;&#26102;&#12290;&#22312;Symfony&#20013;&#65292;&#22788;&#29702;&#23376;&#35831;&#27714;&#21487;&#20197;&#36890;&#36807;&#22810;&#31181;&#26041;&#24335;&#23454;&#29616;&#65292;&#20197;&#19979;&#26159;&#20960;&#31181;&#24120;&#35265;&#30340;&#26041;&#27861;&#65306;<\/p>\n<h3>1. &#20351;&#29992;<code>RequestHandlerInterface<\/code><\/h3>\n<p><code>RequestHandlerInterface<\/code>&#26159;Symfony&#26694;&#26550;&#20013;&#30340;&#19968;&#20010;&#25509;&#21475;&#65292;&#29992;&#20110;&#22788;&#29702;HTTP&#35831;&#27714;&#24182;&#36820;&#22238;&#21709;&#24212;&#12290;&#20320;&#21487;&#20197;&#36890;&#36807;&#23454;&#29616;&#36825;&#20010;&#25509;&#21475;&#26469;&#21019;&#24314;&#33258;&#23450;&#20041;&#30340;&#23376;&#35831;&#27714;&#22788;&#29702;&#22120;&#12290;<\/p>\n<pre><code class=\"language-php\">use SymfonyComponentHttpFoundationRequest;\nuse SymfonyComponentHttpFoundationResponse;\n\nclass MySubRequestHandler implements RequestHandlerInterface\n{\n    public function handle(Request $request): Response\n    {\n        \/\/ &#22788;&#29702;&#23376;&#35831;&#27714;&#30340;&#20855;&#20307;&#36923;&#36753;\n        $response = new Response('This is a sub request response');\n        return $response;\n    }\n}<\/code><\/pre>\n<p>&#28982;&#21518;&#65292;&#20320;&#21487;&#20197;&#23558;&#36825;&#20010;&#22788;&#29702;&#22120;&#27880;&#20876;&#20026;&#26381;&#21153;&#65292;&#24182;&#22312;&#25511;&#21046;&#22120;&#20013;&#20351;&#29992;&#23427;&#12290;<\/p>\n<pre><code class=\"language-yaml\">services:\n    my_sub_request_handler:\n        class: AppHandlerMySubRequestHandler\n\napp.controller.sub_request_controller:\n    arguments:\n        - '@my_sub_request_handler'\n    methods:\n        - ['POST', 'GET']\n    calls:\n        - [handle, [\"@request\"]]<\/code><\/pre>\n<h3>2. &#20351;&#29992;<code>HttpKernel<\/code><\/h3>\n<p>&#22914;&#26524;&#20320;&#24076;&#26395;&#22312;&#19968;&#20010;&#36335;&#30001;&#20013;&#22788;&#29702;&#22810;&#20010;&#23376;&#35831;&#27714;&#65292;&#21487;&#20197;&#20351;&#29992;<code>HttpKernel<\/code>&#12290;<code>HttpKernel<\/code>&#20801;&#35768;&#20320;&#22312;&#21516;&#19968;&#20010;&#35831;&#27714;&#19978;&#19979;&#25991;&#20013;&#22788;&#29702;&#22810;&#20010;&#23376;&#35831;&#27714;&#12290;<\/p>\n<pre><code class=\"language-php\">use SymfonyComponentHttpKernelControllerAbstractController;\nuse SymfonyComponentHttpFoundationRequest;\nuse SymfonyComponentHttpFoundationResponse;\n\nclass SubRequestController extends AbstractController\n{\n    public function index(Request $request)\n    {\n        \/\/ &#22788;&#29702;&#31532;&#19968;&#20010;&#23376;&#35831;&#27714;\n        $subRequest1 = new Request($request-&gt;server-&gt;all(), $request-&gt;query-&gt;all());\n        $subResponse1 = $this-&gt;container-&gt;get('http_kernel')-&gt;handle($subRequest1);\n\n        \/\/ &#22788;&#29702;&#31532;&#20108;&#20010;&#23376;&#35831;&#27714;\n        $subRequest2 = new Request($request-&gt;server-&gt;all(), $request-&gt;query-&gt;all());\n        $subResponse2 = $this-&gt;container-&gt;get('http_kernel')-&gt;handle($subRequest2);\n\n        \/\/ &#32452;&#21512;&#21709;&#24212;\n        $response = new Response();\n        $response-&gt;setContent(json_encode([\n            'sub_response1' =&gt; $subResponse1-&gt;getContent(),\n            'sub_response2' =&gt; $subResponse2-&gt;getContent()\n        ]));\n\n        return $response;\n    }\n}<\/code><\/pre>\n<h3>3. &#20351;&#29992;<code>RoutingContext<\/code><\/h3>\n<p><code>RoutingContext<\/code>&#26159;Symfony&#26694;&#26550;&#20013;&#30340;&#19968;&#20010;&#31867;&#65292;&#29992;&#20110;&#23384;&#20648;&#24403;&#21069;&#35831;&#27714;&#19978;&#19979;&#25991;&#30340;&#20449;&#24687;&#12290;&#20320;&#21487;&#20197;&#36890;&#36807;&#35774;&#32622;<code>RoutingContext<\/code>&#26469;&#25511;&#21046;&#35831;&#27714;&#21644;&#21709;&#24212;&#30340;&#34892;&#20026;&#12290;<\/p>\n<pre><code class=\"language-php\">use SymfonyComponentRoutingRequestContext;\nuse SymfonyComponentHttpFoundationRequest;\nuse SymfonyComponentHttpFoundationResponse;\n\nclass SubRequestController extends AbstractController\n{\n    public function index(Request $request)\n    {\n        \/\/ &#35774;&#32622;&#35831;&#27714;&#19978;&#19979;&#25991;\n        $context = new RequestContext('\/');\n        $context-&gt;setMethod('POST');\n        $context-&gt;setQuery(['param1' =&gt; 'value1']);\n\n        \/\/ &#21019;&#24314;&#19968;&#20010;&#26032;&#30340;&#35831;&#27714;&#23545;&#35937;\n        $newRequest = new Request([], [], [], [], $context);\n\n        \/\/ &#22788;&#29702;&#23376;&#35831;&#27714;\n        $subResponse = $this-&gt;container-&gt;get('http_kernel')-&gt;handle($newRequest);\n\n        \/\/ &#36820;&#22238;&#21709;&#24212;\n        return $subResponse;\n    }\n}<\/code><\/pre>\n<h3>&#31034;&#20363;&#20195;&#30721;<\/h3>\n<p>&#20197;&#19979;&#26159;&#19968;&#20010;&#23436;&#25972;&#30340;&#31034;&#20363;&#65292;&#23637;&#31034;&#20102;&#22914;&#20309;&#22312;Symfony&#20013;&#22788;&#29702;&#23376;&#35831;&#27714;&#65306;<\/p>\n<pre><code class=\"language-php\">\/\/ &#23450;&#20041;&#23376;&#35831;&#27714;&#22788;&#29702;&#22120;\nclass MySubRequestHandler implements RequestHandlerInterface\n{\n    public function handle(Request $request): Response\n    {\n        $response = new Response('This is a sub request response');\n        return $response;\n    }\n}\n\n\/\/ &#27880;&#20876;&#23376;&#35831;&#27714;&#22788;&#29702;&#22120;&#20026;&#26381;&#21153;\nservices:\n    my_sub_request_handler:\n        class: AppHandlerMySubRequestHandler\n\n\/\/ &#25511;&#21046;&#22120;&#22788;&#29702;&#23376;&#35831;&#27714;\napp.controller.sub_request_controller:\n    arguments:\n        - '@my_sub_request_handler'\n    methods:\n        - ['POST', 'GET']\n    calls:\n        - [handle, [\"@request\"]]\n\n\/\/ &#37197;&#32622;&#36335;&#30001;\nroutes:\n    sub_request:\n        path: \/sub-request\n        controller: AppControllerSubRequestController::index<\/code><\/pre>\n<p>&#22312;&#36825;&#20010;&#31034;&#20363;&#20013;&#65292;&#25105;&#20204;&#23450;&#20041;&#20102;&#19968;&#20010;&#31616;&#21333;&#30340;&#23376;&#35831;&#27714;&#22788;&#29702;&#22120;&#65292;&#24182;&#23558;&#20854;&#27880;&#20876;&#20026;&#26381;&#21153;&#12290;&#28982;&#21518;&#65292;&#22312;&#25511;&#21046;&#22120;&#20013;&#20351;&#29992;&#36825;&#20010;&#22788;&#29702;&#22120;&#26469;&#22788;&#29702;&#23376;&#35831;&#27714;&#12290;&#26368;&#21518;&#65292;&#25105;&#20204;&#22312;&#36335;&#30001;&#37197;&#32622;&#20013;&#25351;&#23450;&#20102;&#22788;&#29702;&#23376;&#35831;&#27714;&#30340;&#25511;&#21046;&#22120;&#26041;&#27861;&#12290;<\/p>\n<p>&#36890;&#36807;&#36825;&#20123;&#26041;&#27861;&#65292;&#20320;&#21487;&#20197;&#22312;Symfony&#20013;&#28789;&#27963;&#22320;&#22788;&#29702;&#23376;&#35831;&#27714;&#65292;&#20197;&#28385;&#36275;&#19981;&#21516;&#30340;&#38656;&#27714;&#12290;<\/p>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div><div class=\"wp-block-columns p-0 border is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-columns px-4 py-3 border-bottom has-background is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\" style=\"background:linear-gradient(243deg,rgb(238,238,238) 0%,rgba(58,166,242,0.15) 100%)\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-6c531013 wp-block-group-is-layout-flex\">\r\n<figure class=\"wp-block-image size-thumbnail is-resized is-style-rounded is-style-rounded--2\"><img decoding=\"async\" src=\"https:\/\/www.zhaozhao123.cn\/myitems\/images\/sites16\/2025\/06\/zp-400x300.jpg\" alt=\"&#40657;&#26495;Bug&#35762;&#24072;\" class=\"wp-image-1849\" style=\"object-fit:cover;width:30px;height:30px\"><\/figure>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading my-0\" style=\"font-size:clamp(0.875rem, 0.875rem + ((1vw - 0.2rem) * 0.175), 1rem);\">&#40657;&#26495;Bug&#35762;&#24072;<\/h2>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n\r\n\r\n\r\n<div class=\"wp-block-columns px-xl-5 px-4 py-xl-4 py-3 is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<body><h3>&#20171;&#32461;<\/h3><p>&#23376;&#35831;&#27714;&#26159;Symfony&#24320;&#21457;&#20013;&#30340;&#22522;&#30707;&#65292;&#20026;&#24320;&#21457;&#32773;&#25552;&#20379;&#20102;&#31649;&#29702;&#24182;&#25511;&#21046;&#24212;&#29992;&#20869;&#37096;&#35831;&#27714;&#30340;&#33021;&#21147;&#12290;&#26412;&#25351;&#21335;&#23558;&#24102;&#20320;&#28145;&#20837;&#20102;&#35299;&#23376;&#35831;&#27714;&#30340;&#27010;&#24565;&#12289;&#20854;&#37325;&#35201;&#24615;&#20197;&#21450;&#22914;&#20309;&#22312;&#24744;&#30340;Symfony&#39033;&#30446;&#20013;&#23454;&#29616;&#23427;&#20204;&#12290;&#32487;&#32493;&#38405;&#35835;&#20197;&#20102;&#35299;&#20351;&#29992;&#23376;&#35831;&#27714;&#30340;&#20248;&#21183;&#65292;&#25506;&#32034;&#23454;&#38469;&#31034;&#20363;&#65292;&#24182;&#25104;&#20026;&#33021;&#22815;&#29087;&#32451;&#23558;&#20854;&#34701;&#20837;&#24212;&#29992;&#31243;&#24207;&#30340;&#24378;&#22823;&#21151;&#33021;&#30340;&#19987;&#23478;&#12290;<\/p><h3>Sub-request&#26159;&#25351;&#35831;&#27714;&#30340;&#19968;&#37096;&#20998;&#65292;&#36890;&#24120;&#29992;&#20110;API&#35843;&#29992;&#25110;&#26381;&#21153;&#35843;&#29992;&#26102;&#12290;<\/h3><p>&#22312;Symfony&#20013;&#65292;&#23376;&#35831;&#27714;&#26412;&#36136;&#19978;&#26159;&#22312;&#22788;&#29702;&#21478;&#19968;&#20010;&ldquo;&#20027;&rdquo;&#35831;&#27714;&#26399;&#38388;&#21457;&#20986;&#30340;&#35831;&#27714;&#12290;&#25442;&#21477;&#35805;&#35828;&#65292;&#24744;&#30340;&#24212;&#29992;&#31243;&#24207;&#23454;&#38469;&#19978;&#26159;&#22312;&#21521;&#33258;&#24049;&#25552;&#20986;&#19968;&#20010;&#25805;&#20316;&#65292;&#31867;&#20284;&#20110;&#23427;&#20250;&#22914;&#20309;&#21709;&#24212;&#26469;&#33258;&#27983;&#35272;&#22120;&#30340;&#35831;&#27714;&#12290;&#36825;&#26159;&#19968;&#31181;&#26041;&#20415;&#30340;&#26041;&#27861;&#26469;&#37325;&#29992;&#36923;&#36753;&#24182;&#20174;&#25511;&#21046;&#22120;&#28210;&#26579;&#35270;&#22270;&#25110;&#39029;&#38754;&#30340;&#19968;&#37096;&#20998;&#12290;<\/p><h3>&#20026;&#20160;&#20040;&#20351;&#29992;&#23376;&#35831;&#27714;&#65311;<\/h3><p>&#32452;&#25104;&#65306;&#23427;&#20204;&#20801;&#35768;&#20174;&#22810;&#20010;&#29420;&#31435;&#25511;&#21046;&#30340;&#37096;&#20998;&#65288;&#22914;&#32452;&#20214;&#12289;&#20391;&#36793;&#26639;&#31561;&#65289;&#32452;&#21512;&#39029;&#38754;&#12290;<\/p><p>&#38548;&#31163;&#65306;&#20801;&#35768;&#24212;&#29992;&#31243;&#24207;&#30340;&#37096;&#20998;&#37096;&#20998;&#29420;&#31435;&#36816;&#34892;&#65292;&#36825;&#26377;&#21161;&#20110;&#27979;&#35797;&#21644;&#24320;&#21457;&#12290;<\/p><p>&#21487;&#37325;&#29992;&#24615;&#65306;&#20182;&#20204;&#20419;&#36827;&#29616;&#26377;&#25511;&#21046;&#22120;\/&#21160;&#20316;&#30340;&#22797;&#29992;&#12290;<\/p><h3>&#22522;&#26412;&#23454;&#29616;<\/h3><p>&#20026;&#20102;&#22312;Symfony&#20013;&#21019;&#24314;&#19968;&#20010;&#23376;&#35831;&#27714;&#65292;&#20320;&#38656;&#35201;&#20351;&#29992;&#12290;<code>forward()<\/code>&#22312;&#25511;&#21046;&#22120;&#30340;&#26041;&#27861;&#65292;&#36825;&#26159;Symfony&#30340;&#19968;&#37096;&#20998;&#12290;<code>AbstractController<\/code>&#26041;&#27861;&#20801;&#35768;&#24744;&#23558;&#24403;&#21069;&#35831;&#27714;&#36716;&#21457;&#21040;&#21478;&#19968;&#20010;&#25511;&#21046;&#22120;&#26041;&#27861;&#65292;&#35813;&#26041;&#27861;&#21487;&#20197;&#36820;&#22238;&#21709;&#24212;&#65292;&#24744;&#30340;&#20027;&#21160;&#20316;&#21487;&#20197;&#20351;&#29992;&#25110;&#23558;&#20854;&#25972;&#21512;&#21040;&#26356;&#22823;&#30340;&#21709;&#24212;&#20013;&#12290;<\/p><p>&#31034;&#20363;&#29992;&#20363;&#65306;&#24819;&#35937;&#19968;&#19979;&#65292;&#24744;&#26377;&#19968;&#20010;&#20202;&#34920;&#30424;&#25511;&#21046;&#22120;&#21160;&#20316;&#65292;&#24182;&#19988;&#24819;&#35201;&#21253;&#25324;&#19968;&#20010;&#30001;&#21333;&#29420;&#30340;&#25511;&#21046;&#22120;&#31649;&#29702;&#30340;&#26368;&#26032;&#26032;&#38395;&#37096;&#20998;&#12290;&#32780;&#19981;&#26159;&#37325;&#22797;&#20195;&#30721;&#65292;&#24744;&#21487;&#20197;&#21019;&#24314;&#19968;&#20010;&#23376;&#35831;&#27714;&#21040;&#37027;&#20010;&#19987;&#38376;&#30340;&#26032;&#38395;&#37096;&#20998;&#25511;&#21046;&#22120;&#12290;<\/p><pre><code>public function dashboard(Request $request): Response {\n    \/\/ ... some dashboard logic\n\n    \/\/ Create a sub-request to the news feed controller\n    $response = $this-&gt;forward('AppControllerNewsController::feed', [\n        'max' =&gt; 5\n    ]);\n\n    \/\/ Render the dashboard with the included news feed\n    return $this-&gt;render('dashboard\/dashboard.html.twig', [\n        'news_feed' =&gt; $response-&gt;getContent()\n    ]);\n}\n<\/code><\/pre><p>&#35813;&#26041;&#27861;&#23558;&#35843;&#29992;&#27492;&#26041;&#27861;&#12290;<code>feed<\/code>&#22312;NewsController&#19978;&#25191;&#34892;&#21160;&#20316;&#24182;&#20256;&#36882;&#21442;&#25968;&#12290;<code>max<\/code>&#20540;&#20026;5&#30340;NewsController&#21160;&#20316;&#23558;&#22788;&#29702;&#23376;&#35831;&#27714;&#65292;&#22914;&#21516;&#27491;&#24120;&#30340;&#35831;&#27714;&#19968;&#26679;&#12290;<\/p><h3>&#39640;&#32423;&#29992;&#27861;<\/h3><p>&#22312;&#22522;&#26412;&#23454;&#29616;&#20013;&#35206;&#30422;&#20102;&#35768;&#22810;&#29992;&#20363;&#65292;&#26377;&#26102;&#38656;&#35201;&#23545;&#35831;&#27714;&#36827;&#34892;&#26356;&#20005;&#26684;&#30340;&#25511;&#21046;&#12290;&#20197;&#19979;&#26159;&#25163;&#21160;&#21019;&#24314;&#21644;&#22788;&#29702;&#23376;&#35831;&#27714;&#30340;&#27493;&#39588;&#65306;<\/p><pre><code>\nuse SymfonyComponentHttpFoundationRequest;\nuse SymfonyComponentHttpKernelHttpKernelInterface;\n\npublic function dashboardWithManualSubRequest(Request $request, HttpKernelInterface $httpKernel): Response {\n    \/\/ ... some dashboard logic\n\n    \/\/ Manually create a sub-request\n    $pathInfo = '\/path\/to\/sub\/request';\n    $subRequest = $request-&gt;duplicate(null, null, ['_controller' =&gt; 'AppControllerSomeController::someAction']);\n    $subRequest-&gt;setMethod('GET');\n    $subRequest-&gt;server-&gt;set('PATH_INFO', $pathInfo);\n    $subRequest-&gt;server-&gt;set('REQUEST_URI', $pathInfo);\n\n    \/\/ Handle the sub-request\n    $response = $httpKernel-&gt;handle($subRequest, HttpKernelInterface::SUB_REQUEST);\n\n    \/\/ Render the dashboard with the result of sub-request handling\n    return $this-&gt;render('dashboard\/dashboardWithSubRequest.html.twig', [\n        'sub_content' =&gt; $response-&gt;getContent()\n    ]);\n}\n<\/code><\/pre><p>&#25163;&#21160;&#21019;&#24314;&#23376;&#35831;&#27714;&#21487;&#20197;&#35753;&#20320;&#23450;&#20041;&#26041;&#27861;&#12289;&#21442;&#25968;&#20197;&#21450;&#29978;&#33267;&#20462;&#25913;&#26381;&#21153;&#22120;&#21464;&#37327;&#65292;&#20174;&#32780;&#33719;&#24471;&#28789;&#27963;&#24615;&#12290;<\/p><h3>&#26368;&#20339;&#23454;&#36341;<\/h3><p>&#38480;&#21046;&#20351;&#29992;&#65306;&#34429;&#28982;&#23376;&#35831;&#27714;&#24456;&#26377;&#29992;&#65292;&#20294;&#23427;&#20204;&#19981;&#24212;&#35813;&#36807;&#24230;&#20351;&#29992;&#12290;&#20165;&#22312;&#23427;&#20204;&#24102;&#26469;&#30340;&#22909;&#22788;&#26368;&#22823;&#26102;&#25165;&#24212;&#20351;&#29992;&#23427;&#20204;&#12290;<\/p><p>&#29420;&#31435;&#23376;&#35831;&#27714;&#65306;&#35831;&#35774;&#35745;&#24744;&#30340;&#23376;&#35831;&#27714;&#20197;&#30830;&#20445;&#23427;&#20204;&#19981;&#20381;&#36182;&#20110;&#20027;&#35831;&#27714;&#30340;&#21103;&#20316;&#29992;&#12290;<\/p><p>&#30830;&#20445;&#27599;&#20010;&#23376;&#35831;&#27714;&#30340;&#30446;&#30340;&#28165;&#26224;&#19988;&#26131;&#20110;&#29702;&#35299;&#65292;&#20197;&#20445;&#25345;&#20195;&#30721;&#30340;&#21487;&#35835;&#24615;&#21644;&#31649;&#29702;&#24615;&#12290;<\/p><h3>&#27979;&#35797;&#23376;&#35831;&#27714;<\/h3><p>&#23601;&#20687;&#20195;&#30721;&#20013;&#30340;&#20854;&#20182;&#37096;&#20998;&#19968;&#26679;&#65292;&#23376;&#35831;&#27714;&#20063;&#24212;&#35813;&#36827;&#34892;&#27979;&#35797;&#12290;Symfony&#30340;&#21151;&#33021;&#24615;&#27979;&#35797;&#24037;&#20855;&#20801;&#35768;&#20320;&#22312;&#38548;&#31163;&#25110;&#20316;&#20026;&#26356;&#22823;&#35831;&#27714;&#30340;&#19968;&#37096;&#20998;&#30340;&#24773;&#20917;&#19979;&#27979;&#35797;&#36825;&#20123;&#35831;&#27714;&#30340;&#22788;&#29702;&#12290;<\/p><p>&#31034;&#20363;&#27979;&#35797;&#65306;&#20551;&#35774;&#25105;&#20204;&#26377;&#19968;&#20010;<code>newsAction<\/code>&#25105;&#20204;&#24819;&#35201;&#27979;&#35797;&#30340;&#20869;&#23481;&#22914;&#19979;&#65306;<\/p><pre><code>\nuse SymfonyBundleFrameworkBundleTestWebTestCase;\n\nclass NewsControllerTest extends WebTestCase {\n    public function testFeedAction() {\n        \/\/ Create client and request\n        $client = static::createClient();\n        $crawler = $client-&gt;request('GET', '\/news\/feed');\n\n        \/\/ Assertions\n        $this-&gt;assertResponseIsSuccessful();\n        $this-&gt;assertSelectorTextContains('.news-item', 'Latest news item');\n    }\n}\n<\/code><\/pre><p>&#36825;&#34920;&#26126;&#65292;&#24403;&#23545;&#26032;&#38395;&#27969;&#36335;&#30001;&#21457;&#20986;&#35831;&#27714;&#26102;&#65292;&#21709;&#24212;&#25104;&#21151;&#65292;&#24182;&#19988;&#39029;&#38754;&#19978;&#26377;&#19968;&#20010;&#26032;&#38395;&#26465;&#30446;&#12290;<\/p><h3>&#32467;&#35770;&#12290;<\/h3><p>Symfony&#20013;&#30340;&#23376;&#35831;&#27714;&#26159;&#19968;&#20010;&#24378;&#22823;&#30340;&#29305;&#24615;&#65292;&#23427;&#20801;&#35768;&#24320;&#21457;&#20154;&#21592;&#26500;&#24314;&#22797;&#26434;&#30340;&#20294;&#32452;&#32455;&#33391;&#22909;&#30340;Web&#24212;&#29992;&#31243;&#24207;&#12290;&#36890;&#36807;&#21033;&#29992;&#23376;&#35831;&#27714;&#65292;&#24744;&#21487;&#20197;&#20445;&#25345;&#20195;&#30721;&#31616;&#27905;&#24182;&#32500;&#25252;&#28165;&#26224;&#30340;&#26550;&#26500;&#12290;&#22312;&#24744;&#30340;&#39033;&#30446;&#20013;&#23454;&#39564;&#20351;&#29992;&#23376;&#35831;&#27714;&#65292;&#24182;&#25317;&#25265;&#23427;&#20204;&#40723;&#21169;&#30340;&#27169;&#22359;&#21270;&#35774;&#35745;&#21407;&#21017;&#12290;&#20973;&#20511;&#26412;&#25351;&#21335;&#65292;&#24744;&#23558;&#36208;&#19978;&#22788;&#29702;Symfony&#20013;&#23376;&#35831;&#27714;&#30340;&#22797;&#26434;&#32454;&#33410;&#30340;&#36947;&#36335;&#65281;<\/p><\/body>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div><div class=\"wp-block-columns p-0 border is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-columns px-4 py-3 border-bottom has-background is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\" style=\"background:linear-gradient(243deg,rgb(238,238,238) 0%,rgba(58,166,242,0.15) 100%)\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-6c531013 wp-block-group-is-layout-flex\">\r\n<figure class=\"wp-block-image size-thumbnail is-resized is-style-rounded is-style-rounded--3\"><img decoding=\"async\" src=\"https:\/\/www.zhaozhao123.cn\/myitems\/images\/sites16\/2025\/06\/xygcfznnzczhsdmwydzhsmzzzmddnq-400x300.jpg\" alt=\"&#38271;&#24037;&#30721;&#22900;1523\" class=\"wp-image-2906\" style=\"object-fit:cover;width:30px;height:30px\"><\/figure>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading my-0\" style=\"font-size:clamp(0.875rem, 0.875rem + ((1vw - 0.2rem) * 0.175), 1rem);\">&#38271;&#24037;&#30721;&#22900;1523<\/h2>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n\r\n\r\n\r\n<div class=\"wp-block-columns px-xl-5 px-4 py-xl-4 py-3 is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<p>&#22312; Symfony &#20013;&#22788;&#29702;&#23376;&#35831;&#27714;&#26159;&#19968;&#31181;&#24120;&#35265;&#30340;&#32534;&#31243;&#23454;&#36341;&#65292;&#29305;&#21035;&#26159;&#22312;&#38656;&#35201;&#20998;&#21457;&#35831;&#27714;&#21040;&#19981;&#21516;&#30340;&#32452;&#20214;&#26102;&#12290;&#36825;&#19981;&#20165;&#21487;&#20197;&#25552;&#39640;&#31995;&#32479;&#24615;&#33021;&#65292;&#36824;&#33021;&#22686;&#24378;&#24212;&#29992;&#30340;&#28789;&#27963;&#24615;&#21644;&#25193;&#23637;&#24615;&#12290;<\/p>\n<h3>&#20160;&#20040;&#26159;&#23376;&#35831;&#27714;&#65311;<\/h3>\n<p>&#23376;&#35831;&#27714;&#26159; Symfony &#24212;&#29992;&#31243;&#24207;&#20013;&#30340;&#29305;&#27530;&#31867;&#22411;&#65292;&#23427;&#20204;&#20195;&#34920;&#20102;&#20174;&#26576;&#20010;&#25511;&#21046;&#22120;&#25110;&#26381;&#21153;&#36820;&#22238;&#30340;&#21333;&#29420;&#35831;&#27714;&#12290;&#36825;&#20123;&#35831;&#27714;&#21487;&#20197;&#29420;&#31435;&#20110;&#20854;&#20182;&#35831;&#27714;&#25191;&#34892;&#65292;&#24182;&#19988;&#36890;&#24120;&#29992;&#20110;&#22788;&#29702;&#29305;&#23450;&#30340;&#36923;&#36753;&#25110;&#32773;&#25968;&#25454;&#35775;&#38382;&#25805;&#20316;&#12290;<\/p>\n<h3>&#22914;&#20309;&#22312; Symfony &#20013;&#21019;&#24314;&#23376;&#35831;&#27714;<\/h3>\n<h4>&#21019;&#24314;&#23376;&#35831;&#27714;&#23454;&#20363;<\/h4>\n<p>&#39318;&#20808;&#65292;&#20320;&#38656;&#35201;&#23450;&#20041;&#19968;&#20010;&#25511;&#21046;&#22120;&#26469;&#22788;&#29702;&#23376;&#35831;&#27714;&#12290;&#36825;&#20010;&#25511;&#21046;&#22120;&#23558;&#36127;&#36131;&#29983;&#25104;&#24182;&#31649;&#29702;&#23376;&#35831;&#27714;&#23454;&#20363;&#12290;&#20197;&#19979;&#26159;&#19968;&#20010;&#31616;&#21333;&#30340;&#20363;&#23376;&#65306;<\/p>\n<pre><code class=\"language-php\">&lt;?php\nnamespace AppController;\n\nuse SymfonyComponentHttpFoundationRequest;\nuse SymfonyBundleFrameworkBundleControllerAbstractController;\n\nclass MySubRequestController extends AbstractController\n{\n    public function create(Request $request)\n    {\n        \/\/ &#20551;&#35774;&#25105;&#20204;&#26377;&#19968;&#20010;&#21517;&#20026; 'my_service' &#30340;&#26381;&#21153;&#65292;&#23427;&#22788;&#29702;&#26469;&#33258;&#23458;&#25143;&#31471;&#30340;&#35831;&#27714;\n        $service = $this-&gt;get('my_service');\n\n        \/\/ &#21019;&#24314;&#19968;&#20010;&#26032;&#30340;&#23376;&#35831;&#27714;&#23454;&#20363;\n        $subRequest = new AppMySubRequest();\n\n        \/\/ &#23558;&#23376;&#35831;&#27714;&#23454;&#20363;&#19982;&#35831;&#27714;&#20851;&#32852;&#36215;&#26469;\n        $request-&gt;setChild($subRequest);\n\n        return $this-&gt;redirectToRoute('your_route', [\n            'id' =&gt; $subRequest-&gt;getId(),\n        ]);\n    }\n}<\/code><\/pre>\n<p>&#22312;&#36825;&#20010;&#20363;&#23376;&#20013;&#65292;<code>create()<\/code> &#26041;&#27861;&#25509;&#21463;&#19968;&#20010; <code>Request<\/code> &#23545;&#35937;&#20316;&#20026;&#21442;&#25968;&#65292;&#24182;&#20351;&#29992; <code>get('my_service')<\/code> &#33719;&#21462;&#21040;&#30340;&#26381;&#21153;&#23545;&#35937;&#12290;&#28982;&#21518;&#65292;&#36890;&#36807;&#35843;&#29992; <code>new AppMySubRequest()<\/code> &#21019;&#24314;&#19968;&#20010;&#26032;&#30340;&#23376;&#35831;&#27714;&#23454;&#20363;&#65292;&#24182;&#23558;&#20854;&#19982;&#21407;&#22987;&#35831;&#27714;&#20851;&#32852;&#36215;&#26469;&#12290;<\/p>\n<h4>&#22788;&#29702;&#23376;&#35831;&#27714;&#23454;&#20363;<\/h4>\n<p>&#19968;&#26086;&#21019;&#24314;&#20102;&#23376;&#35831;&#27714;&#23454;&#20363;&#65292;&#23601;&#21487;&#20197;&#20687;&#26222;&#36890;&#35831;&#27714;&#19968;&#26679;&#23545;&#20854;&#36827;&#34892;&#22788;&#29702;&#12290;&#20363;&#22914;&#65292;&#20320;&#21487;&#20197;&#20351;&#29992; <code>$subRequest<\/code> &#26469;&#33719;&#21462;&#35831;&#27714;&#30340;&#30456;&#20851;&#20449;&#24687;&#65292;&#36827;&#34892;&#36827;&#19968;&#27493;&#30340;&#25968;&#25454;&#22788;&#29702;&#65292;&#25110;&#32773;&#21709;&#24212;&#32473;&#23458;&#25143;&#31471;&#12290;<\/p>\n<h3>&#31034;&#20363;&#20195;&#30721;<\/h3>\n<p>&#20551;&#35774;&#25105;&#20204;&#26377;&#22914;&#19979;&#31867;&#65306;<\/p>\n<pre><code class=\"language-php\">&lt;?php\nnamespace AppHttpControllers;\n\nuse AppMySubRequest;\n\nclass MyController\n{\n    \/**\n     * Handle the incoming request.\n     *\n     * @param Request $request The incoming HTTP request.\n     *\/\n    public function handleRequest(Request $request)\n    {\n        $subRequest = $request-&gt;getChild(MySubRequest::class);\n\n        \/\/ &#22312;&#36825;&#37324;&#65292;&#20320;&#21487;&#20197;&#26681;&#25454;&#23376;&#35831;&#27714;&#30340; ID &#21644;&#20854;&#20182;&#30456;&#20851;&#20449;&#24687;&#26469;&#36827;&#34892;&#25805;&#20316;\n        echo \"Handling sub-request with id: {$subRequest-&gt;getId()}n\";\n\n        \/\/ &#22914;&#26524;&#38656;&#35201;&#65292;&#20320;&#21487;&#20197;&#22312;&#36825;&#37324;&#23545;&#23376;&#35831;&#27714;&#36827;&#34892;&#19968;&#20123;&#39069;&#22806;&#30340;&#25805;&#20316;\n        \/\/ ...\n    }\n}<\/code><\/pre>\n<p>&#22312;&#36825;&#20010;&#20363;&#23376;&#20013;&#65292;<code>handleRequest()<\/code> &#26041;&#27861;&#25509;&#25910;&#19968;&#20010; <code>Request<\/code> &#23545;&#35937;&#65292;&#20854;&#20013;&#21253;&#21547;&#20102;&#25351;&#21521;&#23376;&#35831;&#27714;&#23454;&#20363;&#30340;&#24341;&#29992;&#12290;&#28982;&#21518;&#65292;&#25105;&#20204;&#21487;&#20197;&#20687;&#22788;&#29702;&#20219;&#20309;&#35831;&#27714;&#19968;&#26679;&#26469;&#25805;&#20316;&#23376;&#35831;&#27714;&#23454;&#20363;&#12290;<\/p>\n<h3>&#24635;&#32467;<\/h3>\n<p>&#22312; Symfony &#20013;&#22788;&#29702;&#23376;&#35831;&#27714;&#26159;&#19968;&#31181;&#24378;&#22823;&#30340;&#24037;&#20855;&#65292;&#21487;&#20197;&#24110;&#21161;&#20320;&#26356;&#26377;&#25928;&#22320;&#32452;&#32455;&#21644;&#31649;&#29702;&#35831;&#27714;&#12290;&#36890;&#36807;&#19978;&#36848;&#30340;&#31034;&#20363;&#20195;&#30721;&#65292;&#20320;&#24212;&#35813;&#24050;&#32463;&#20102;&#35299;&#20102;&#22914;&#20309;&#21019;&#24314;&#21644;&#22788;&#29702;&#23376;&#35831;&#27714;&#23454;&#20363;&#12290;&#24076;&#26395;&#36825;&#23545;&#24744;&#26377;&#25152;&#24110;&#21161;&#65281;<\/p>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Bug&#32534;&#35793;&#29422; &#26631;&#39064;&#65306;&#22312;Symfony&#20013;&#22788;&#29702;&#23376;&#35831;&#27714;&#65306;&#23454;&#29992;&#25351;&#21335; &#038;#22..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false},"tags":[],"my1js2nav":[45],"tuisongtax":[],"class_list":["post-1081","my1js","type-my1js","status-publish","hentry","my1js2nav-symfony"],"acf":{"qian_art_seotitle":"","qian_art_seotitle_source":{"label":"SEO\u6807\u9898","type":"text","formatted_value":""},"qian_art_seokws":"","qian_art_seokws_source":{"label":"SEO\u5173\u952e\u8bcd","type":"text","formatted_value":""},"qian_art_stzhong":"","qian_art_stzhong_source":{"label":"\u4e2d | \u77ed\u6807\u9898","type":"text","formatted_value":""}},"_links":{"self":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js\/1081","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js"}],"about":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/types\/my1js"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/users\/1"}],"wp:attachment":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/media?parent=1081"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tags?post=1081"},{"taxonomy":"my1js2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js2nav?post=1081"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tuisongtax?post=1081"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}