{"id":757,"date":"2025-06-11T22:57:36","date_gmt":"2025-06-11T14:57:36","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/php\/php-application-manual\/laravel\/757.html"},"modified":"2025-06-11T22:57:36","modified_gmt":"2025-06-11T14:57:36","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-laravel-%e4%b8%ad%e5%ae%9e%e7%8e%b0%e9%99%90%e6%b5%81","status":"publish","type":"my1js","link":"https:\/\/www.zhaozhao123.cn\/php\/my1js\/757.html","title":{"rendered":"\u5982\u4f55\u5728 Laravel \u4e2d\u5b9e\u73b0\u9650\u6d41"},"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>&#22312; Laravel &#20013;&#23454;&#29616;&#38480;&#27969;&#65288;Rate Limiting&#65289;&#21487;&#20197;&#36890;&#36807;&#22810;&#31181;&#26041;&#24335;&#23454;&#29616;&#65292;&#20854;&#20013;&#26368;&#24120;&#29992;&#30340;&#26159;&#20351;&#29992; <code>IlluminateRoutingMiddlewareThrottleRequests<\/code> &#20013;&#38388;&#20214;&#12290;&#36825;&#20010;&#20013;&#38388;&#20214;&#21487;&#20197;&#24110;&#21161;&#20320;&#22312;&#24212;&#29992;&#30340;&#21508;&#20010;&#36335;&#30001;&#20013;&#35774;&#32622;&#35831;&#27714;&#36895;&#29575;&#38480;&#21046;&#12290;<\/p>\n<h3>1. &#23433;&#35013;&#21644;&#37197;&#32622; Throttle Requests<\/h3>\n<p>&#39318;&#20808;&#65292;&#20320;&#38656;&#35201;&#23433;&#35013; <code>throttlerequests<\/code> &#21253;&#65306;<\/p>\n<pre><code class=\"language-bash\">composer require laravel\/traffic<\/code><\/pre>\n<p>&#28982;&#21518;&#65292;&#23558;&#36825;&#20010;&#21253;&#28155;&#21152;&#21040;&#20320;&#30340;&#26381;&#21153;&#25552;&#20379;&#32773;&#20013;&#65306;<\/p>\n<pre><code class=\"language-php\">\/\/ config\/app.php\n'providers' =&gt; [\n    \/\/ ...\n    AppProvidersTrafficServiceProvider::class,\n],<\/code><\/pre>\n<p>&#25509;&#19979;&#26469;&#65292;&#36816;&#34892;&#20197;&#19979;&#21629;&#20196;&#26469;&#21457;&#24067;&#37197;&#32622;&#25991;&#20214;&#24182;&#21019;&#24314;&#25968;&#25454;&#24211;&#34920;&#65306;<\/p>\n<pre><code class=\"language-bash\">php artisan vendor:publish --provider=\"AppProvidersTrafficServiceProvider\"\nphp artisan migrate<\/code><\/pre>\n<h3>2. &#21019;&#24314;&#33258;&#23450;&#20041;&#38480;&#27969;&#35268;&#21017;<\/h3>\n<p>&#20320;&#21487;&#20197;&#26681;&#25454;&#38656;&#35201;&#21019;&#24314;&#33258;&#23450;&#20041;&#38480;&#27969;&#35268;&#21017;&#12290;&#20363;&#22914;&#65292;&#22914;&#26524;&#20320;&#24819;&#23545;&#27599;&#20010; IP &#22320;&#22336;&#27599;&#20998;&#38047;&#21482;&#20801;&#35768;&#35775;&#38382;&#19968;&#27425;&#65292;&#21487;&#20197;&#36825;&#26679;&#20570;&#65306;<\/p>\n<pre><code class=\"language-php\">use IlluminateSupportFacadesRateLimiter;\n\nRateLimiter::for('ip', function ($request) {\n    return $request-&gt;ip();\n}, 60);<\/code><\/pre>\n<h3>3. &#20351;&#29992; Throttle Requests &#20013;&#38388;&#20214;<\/h3>\n<p>&#22312;&#20320;&#30340;&#25511;&#21046;&#22120;&#26041;&#27861;&#19978;&#20351;&#29992; <code>throttle<\/code> &#26041;&#27861;&#26469;&#24212;&#29992;&#38480;&#27969;&#35268;&#21017;&#65306;<\/p>\n<pre><code class=\"language-php\">namespace AppHttpControllers;\n\nuse IlluminateHttpRequest;\nuse IlluminateSupportFacadesRateLimiter;\n\nclass ExampleController extends Controller\n{\n    public function index(Request $request)\n    {\n        RateLimiter::for('ip', function ($request) {\n            return $request-&gt;ip();\n        }, 60);\n\n        if (RateLimiter::tooManyAttempts('ip', 5)) {\n            return response()-&gt;json(['message' =&gt; 'Too many attempts'], 429);\n        }\n\n        \/\/ &#22788;&#29702;&#35831;&#27714;&#36923;&#36753;\n        return response()-&gt;json(['message' =&gt; 'Request processed']);\n    }\n}<\/code><\/pre>\n<h3>4. &#31034;&#20363;&#20195;&#30721;&#27493;&#39588;<\/h3>\n<h4>4.1 &#21019;&#24314;&#33258;&#23450;&#20041;&#38480;&#27969;&#35268;&#21017;<\/h4>\n<pre><code class=\"language-php\">\/\/ app\/Routes\/web.php\nRoute::get('\/example', [ExampleController::class, 'index'])-&gt;middleware('throttle:ip,60');<\/code><\/pre>\n<h4>4.2 &#37197;&#32622; Throttle Requests &#20013;&#38388;&#20214;<\/h4>\n<pre><code class=\"language-php\">\/\/ config\/traffic.php\nreturn [\n    'limits' =&gt; [\n        'ip' =&gt; [\n            60 =&gt; 5, \/\/ &#27599;&#20010; IP &#22320;&#22336;&#27599;&#20998;&#38047;&#26368;&#22810; 5 &#27425;&#35831;&#27714;\n        ],\n    ],\n];<\/code><\/pre>\n<h4>4.3 &#27979;&#35797;&#38480;&#27969;<\/h4>\n<p>&#25171;&#24320;&#27983;&#35272;&#22120;&#24182;&#35775;&#38382; <code>\/example<\/code> &#36335;&#30001;&#65292;&#22914;&#26524; IP &#22320;&#22336;&#24050;&#32463;&#36229;&#36807;&#20102; 5 &#27425;&#35831;&#27714;&#65292;&#21017;&#20250;&#25910;&#21040; 429 &#29366;&#24577;&#30721;&#12290;<\/p>\n<p>&#36890;&#36807;&#20197;&#19978;&#27493;&#39588;&#65292;&#20320;&#21487;&#20197;&#22312; Laravel &#20013;&#25104;&#21151;&#23454;&#29616;&#38480;&#27969;&#21151;&#33021;&#12290;&#20320;&#21487;&#20197;&#26681;&#25454;&#20855;&#20307;&#38656;&#27714;&#35843;&#25972;&#38480;&#27969;&#35268;&#21017;&#21644;&#21442;&#25968;&#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><h2>&#27010;&#35272;<\/h2><p>Laravel &#26159;&#26368;&#28789;&#27963;&#19988;&#21151;&#33021;&#24378;&#22823;&#30340;PHP&#26694;&#26550;&#20043;&#19968;&#65292;&#23427;&#33258;&#24102;&#24378;&#22823;&#29305;&#24615;&#12290;&#20854;&#20013;&#19968;&#39033;&#37325;&#35201;&#21151;&#33021;&#23601;&#26159;&#38480;&#27969;&#33021;&#21147;&#65292;&#21487;&#20197;&#24110;&#21161;&#20445;&#25252;API&#20813;&#21463;&#36807;&#22810;&#35831;&#27714;&#30340;&#20914;&#20987;&#12290;&#26412;&#25945;&#31243;&#23558;&#25351;&#23548;&#24744;&#22312;Laravel&#24212;&#29992;&#20013;&#23454;&#29616;&#38480;&#27969;&#21151;&#33021;&#12290;<\/p><h3>&#29702;&#35299;&#38480;&#27969;&#65288;Rate Limiting&#65289;<\/h3><p>API&#24320;&#21457;&#21644;&#32500;&#25252;&#20013;&#65292;&#38480;&#27969;&#65288;rate limiting&#65289;&#26159;&#19968;&#39033;&#33267;&#20851;&#37325;&#35201;&#30340;&#26041;&#38754;&#12290;&#23427;&#30830;&#20445;&#24212;&#29992;&#22312;&#25351;&#23450;&#30340;&#26102;&#38388;&#33539;&#22260;&#20869;&#33021;&#22815;&#22788;&#29702;&#19968;&#23450;&#25968;&#37327;&#30340;&#35831;&#27714;&#12290;&#36825;&#19981;&#20165;&#38450;&#27490;&#28389;&#29992;&#34892;&#20026;&#65292;&#36824;&#33021;&#24110;&#21161;&#20445;&#25345;&#25152;&#26377;&#29992;&#25143;&#23545;API&#30340;&#24615;&#33021;&#21644;&#21487;&#29992;&#24615;&#12290;<\/p><h2>&#35774;&#32622;&#20013;&#38388;&#20214;<\/h2><p>&#22312;Laravel&#20013;&#65292;&#38480;&#27969;&#21151;&#33021;&#36890;&#36807;&#20013;&#38388;&#20214;&#23454;&#29616;&#12290;Laravel&#20013;&#30340;&#20013;&#38388;&#20214;&#25552;&#20379;&#20102;&#19968;&#20010;&#26041;&#20415;&#30340;&#26426;&#21046;&#26469;&#36807;&#28388;&#36827;&#20837;&#24212;&#29992;&#31243;&#24207;&#30340;HTTP&#35831;&#27714;&#12290;<\/p><p>&#20026;&#20102;&#24320;&#22987;&#65292;&#23548;&#33322;&#21040;<code>app\/Http\/Kernel.php<\/code>&#25991;&#20214;&#12290;&#22312;&#36825;&#37324;&#65292;&#24744;&#23558;&#22312;&#38480;&#27969;&#20013;&#38388;&#20214;&#20013;&#23450;&#20041;&#24744;&#30340;&#36895;&#29575;&#38480;&#21046;&#20013;&#38388;&#20214;&#12290;<\/p><p>&#36335;&#30001;&#20013;&#38388;&#20214;&#25968;&#32452;&#65306;<\/p><pre><code>protected $ routeMiddleware = [\n    'auth' =&gt; AppHttpMiddlewareAuthenticate::class,\n    \/\/ ...\n    'throttle' =&gt; IlluminateRoutingMiddlewareThrottleRequests::class,\n];<\/code><\/pre><p>&#36825;&#26159;&#40664;&#35748;&#30340;&#12290;<code>throttle<\/code>&#20013;&#38388;&#20214;&#20351;&#29992;Laravel&#32531;&#23384;&#26469;&#36319;&#36394;&#29992;&#25143;&#22312;&#19968;&#23450;&#26102;&#26399;&#20869;&#21457;&#20986;&#30340;&#35831;&#27714;&#25968;&#37327;&#12290;<\/p><h2>&#38480;&#27969;&#36335;&#30001;<\/h2><p>&#20026;&#20102;&#24212;&#29992;&#38480;&#27969;&#31574;&#30053;&#21040;&#36335;&#30001;&#65292;&#24744;&#38656;&#35201;&#22312;&#35201;&#38480;&#21046;&#30340;&#36335;&#30001;&#25110;&#19968;&#32452;&#36335;&#30001;&#19978;&#20351;&#29992;&#20013;&#38388;&#20214;&#26041;&#27861;&#12290;<\/p><pre><code>Route::middleware('throttle:60,1')-&gt;group(function () {\n    Route::get('\/my-api', 'ApiController@index');\n    \/\/ Add your rate limited routes here\n});<\/code><\/pre><p>&#19978;&#36848;&#20195;&#30721;&#29255;&#27573;&#21578;&#35785;Laravel&#20801;&#35768;&#27599;&#20998;&#38047;&#26368;&#22810;60&#27425;&#35831;&#27714;&#65288;&#21363;&#27599;&#31186;&#19968;&#27425;&#35831;&#27714;&#65289;&#12290;<code>\/my-api<\/code>&#36335;&#32447;&#12290;<\/p><h2>&#33258;&#23450;&#20041;&#36895;&#29575;&#38480;&#21046;<\/h2><p>&#22312;&#38656;&#35201;&#20026;&#19981;&#21516;&#36335;&#30001;&#23450;&#20041;&#19981;&#21516;&#38480;&#27969;&#35268;&#21017;&#30340;&#24773;&#20917;&#19979;&#65292;&#20320;&#21487;&#20197;&#33258;&#24049;&#23450;&#20041;&#12290;<\/p><pre><code>use IlluminateRoutingMiddlewareThrottleRequests;\n\nRoute::middleware('throttle:100,5')-&gt;group(function () {\n    Route::get('\/another-api', 'AnotherApiController@index');\n});<\/code><\/pre><p>&#36825;&#35774;&#32622;&#20102;&#27599;&#20116;&#20998;&#38047;&#20869;&#35831;&#27714;&#30340;&#26368;&#22823;&#38480;&#21046;&#20026;&#19968;&#30334;&#27425;&#12290;<\/p><h2>&#21019;&#24314;&#33258;&#24049;&#30340;&#20013;&#38388;&#20214;<\/h2><p>&#22914;&#26524;&#40664;&#35748;&#30340;&#33410;&#27969;&#35774;&#32622;&#19981;&#31526;&#21512;&#24744;&#30340;&#38656;&#27714;&#65292;&#24744;&#21487;&#20197;&#21019;&#24314;&#33258;&#23450;&#20041;&#20013;&#38388;&#20214;&#12290;<\/p><pre><code>php artisan make:middleware CustomThrottleMiddleware<\/code><\/pre><p>&#21019;&#24314;&#22909;&#20013;&#38388;&#20214;&#21518;&#65292;&#38656;&#35201;&#22312;HTTP&#26680;&#20013;&#27880;&#20876;&#23427;&#65306;<\/p><pre><code>protected $ routeMiddleware = [\n    \/\/ ...\n    'customThrottle' =&gt; AppHttpMiddlewareCustomThrottleMiddleware::class,\n];<\/code><\/pre><p>&#22312;&#23450;&#21046;&#23454;&#29616;&#20013;&#65292;&#20320;&#21487;&#20197;&#25805;&#32437;&#35831;&#27714;&#25110;&#33258;&#23450;&#20041;&#29992;&#20110;&#35745;&#31639;&#38480;&#27969;&#36923;&#36753;&#12290;<\/p><h2>&#21160;&#24577;&#36895;&#29575;&#38480;&#21046;<\/h2><p>Laravel &#20801;&#35768;&#24744;&#23454;&#29616;&#21160;&#24577;&#36895;&#29575;&#38480;&#21046;&#65292;&#35813;&#38480;&#21046;&#20250;&#26681;&#25454;&#24050;&#35748;&#35777;&#30340;&#29992;&#25143;&#36827;&#34892;&#35843;&#25972;&#65306;<\/p><pre><code>Route::middleware(['auth', 'throttle:rate_limit,1'])-&gt;group(function () {\n    \/\/ Protected routes here\n});\n\n\n\/\/ In 'AppServiceProvider':\npublic function boot()\n{\n    RateLimiter::for('rate_limit', function (Request $request) {\n        return Limit::perMinute(100)-&gt;by(optional($request-&gt;user())-&gt;id ?: $request-&gt;ip());\n    });\n}<\/code><\/pre><p>&#22312;&#36825;&#19968;&#20363;&#23376;&#20013;&#65292;&#24744;&#23450;&#20041;&#20102;<code>rate_limit<\/code>&#26681;&#25454;&#29992;&#25143;&#30340;ID&#25110;&#22914;&#26524;&#20182;&#20204;&#26410;&#35748;&#35777;&#21017;&#20351;&#29992;IP&#22320;&#22336;&#65292;&#20026;&#27599;&#20301;&#29992;&#25143;&#25552;&#20379;&#29420;&#29305;&#30340;&#20307;&#39564;&#12290;<\/p><h2>&#38169;&#35823;&#22788;&#29702;&#21644;&#21709;&#24212;<\/h2><p>&#24403;&#38480;&#27969;&#34987;&#36229;&#36807;&#26102;&#65292;<code>429 Too Many Requests<\/code>HTTP&#29366;&#24577;&#30721;&#36820;&#22238;&#20102;&#12290;&#21487;&#20197;&#36890;&#36807;&#37325;&#20889;&#27492;&#34892;&#20026;&#26469;&#33258;&#23450;&#20041;&#36825;&#31181;&#34892;&#20026;&#65292;&#36890;&#36807;&#35206;&#30422;&#12290;<code>handle<\/code>&#22312;&#20320;&#30340; throttle &#20013;&#38388;&#20214;&#26041;&#27861;&#20013;&#65306;<\/p><pre><code>public function handle($request, Closure $next, $maxAttempts = 60, $decayMinutes = 1)\n{\n    \/\/ Custom logic here\n}<\/code><\/pre><h2>&#23454;&#26045;&#22522;&#20110;Laravel API&#36164;&#28304;&#30340;&#36895;&#29575;&#38480;&#21046;<\/h2><p>&#22312;Laravel&#30340;API&#36164;&#28304;&#19978;&#23454;&#26045;&#36895;&#29575;&#38480;&#21046;&#26159;&#19968;&#20010;&#31616;&#21333;&#30340;&#36807;&#31243;&#65292;&#21487;&#20197;&#38750;&#24120;&#26377;&#25928;&#22320;&#36890;&#36807;Laravel&#20869;&#32622;&#21151;&#33021;&#26469;&#31649;&#29702;&#12290;&#36825;&#37324;&#26377;&#19968;&#20010;&#31616;&#21333;&#30340;&#20363;&#23376;&#26469;&#35828;&#26126;&#22914;&#20309;&#22312;Laravel&#20013;&#23454;&#29616;API&#30340;&#36895;&#29575;&#38480;&#21046;&#12290;<\/p><h3>&#27493;&#39588;1&#65306;&#23450;&#20041;&#36335;&#32447;<\/h3><p>&#39318;&#20808;&#65292;&#20320;&#38656;&#35201;&#23450;&#20041;&#20320;&#30340;API&#31471;&#28857;&#12290;&#36825;&#36890;&#24120;&#26159;&#22312;&#20195;&#30721;&#20013;&#23454;&#29616;&#30340;&#12290;<code>routes\/api.php<\/code>&#25991;&#20214;&#12290;<\/p><pre><code>use AppHttpControllersApiUserController;\r\n\r\nRoute::apiResource('users', UserController::class);\r\n<\/code><\/pre><h3>&#27493;&#39588; 2&#65306;&#20351;&#29992;&#20013;&#38388;&#20214;&#36827;&#34892;&#38480;&#27969;&#22788;&#29702;<\/h3><p>Laravel &#25552;&#20379;&#20102;&#19968;&#20010;<code>throttle<\/code>&#38480;&#27969;&#20013;&#38388;&#20214;&#65288;middleware for rate limiting&#65289;&#12290;&#20320;&#21487;&#20197;&#23558;&#27492;&#20013;&#38388;&#20214;&#24212;&#29992;&#20110;&#20320;&#30340;&#36335;&#30001;&#65292;&#20197;&#38480;&#21046;&#29992;&#25143;&#22312;&#29305;&#23450;&#26102;&#38388;&#33539;&#22260;&#20869;&#21487;&#20197;&#21457;&#20986;&#30340;&#35831;&#27714;&#25968;&#37327;&#12290;<\/p><p>&#20320;&#21487;&#20197;&#36873;&#25321;&#30452;&#25509;&#24212;&#29992;&#20013;&#38388;&#20214;&#21040;&#19968;&#20010;&#36335;&#30001;&#25110;&#19968;&#32452;&#36335;&#30001;&#65306;<\/p><pre><code>Route::middleware('throttle:60,1')-&gt;apiResource('users', UserController::class);\r\n<\/code><\/pre><p>&#22312;&#36825;&#19968;&#20363;&#23376;&#20013;&#65292;<code>throttle:60,1<\/code>&#27599;&#20998;&#38047;&#26368;&#22810;&#20801;&#35768;&#21457;&#20986;60&#20010;&#35831;&#27714;&#12290;<\/p><h3>&#27493;&#39588; 3&#65306;&#33258;&#23450;&#20041;&#36895;&#29575;&#38480;&#21046;<\/h3><p>&#22914;&#26524;&#24744;&#38656;&#35201;&#26356;&#22810;&#30340;&#25511;&#21046;&#65292;&#24744;&#21487;&#20197;&#23450;&#20041;&#36895;&#29575;&#38480;&#21046;&#36923;&#36753;&#12290;<code>Kernel.php<\/code>&#25991;&#20214;&#20301;&#20110;&#12290;<code>app\/Http<\/code>&#30446;&#24405;&#12290;<\/p><p>&#23545;&#19981;&#36215;&#65292;&#24744;&#30340;&#28040;&#24687;&#27809;&#26377;&#25509;&#25910;&#21040;&#12290;&#35831;&#37325;&#26032;&#23581;&#35797;&#12290;<code>$routeMiddleware<\/code>&#25265;&#27465;&#65292;&#25105;&#19981;&#22826;&#26126;&#30333;&#24744;&#30340;&#24847;&#24605;&#65292;&#35831;&#37325;&#26032;&#25551;&#36848;&#24744;&#38656;&#35201;&#32763;&#35793;&#30340;&#20869;&#23481;&#12290;<code>Kernel.php<\/code>&#24744;&#21487;&#20197;&#36890;&#36807;&#28155;&#21152;&#33258;&#23450;&#20041;&#33410;&#27969;&#38190;&#26469;&#28155;&#21152;&#24744;&#30340;&#33258;&#23450;&#20041;&#33410;&#27969;&#38190;&#65306;<\/p><pre><code>protected $routeMiddleware = [\r\n    \/\/ ...\r\n    'customThrottle' =&gt; IlluminateRoutingMiddlewareThrottleRequests::class,\r\n];\r\n<\/code><\/pre><p>&#28982;&#21518;&#22312;&#36335;&#30001;&#20013;&#20351;&#29992;&#23427;&#65306;<\/p><pre><code>Route::middleware('customThrottle:requestsPerMinute,decayMinutes')-&gt;apiResource('users', UserController::class);\r\n<\/code><\/pre><h3>&#31532;4&#27493;&#65306;&#22788;&#29702;&#36229;&#26102;&#24322;&#24120;<\/h3><p>&#24403;&#29992;&#25143;&#36229;&#20986;&#20102;&#36895;&#29575;&#38480;&#21046;&#26102;&#65292;Laravel &#20250;&#25243;&#20986;&#19968;&#20010;&#38169;&#35823;&#12290;<code>ThrottleRequestsException<\/code>&#24744;&#21487;&#20197;&#33258;&#23450;&#20041;&#27492;&#24322;&#24120;&#21709;&#24212;&#65292;&#36890;&#36807;&#32534;&#36753;&#12290;<code>render<\/code>&#26041;&#27861;&#12290;<code>AppExceptionsHandler<\/code>&#24050;&#32463;&#25910;&#21040;&#28040;&#24687;&#12290;<\/p><pre><code>public function render($request, Throwable $exception)\r\n{\r\n    if ($exception instanceof IlluminateHttpExceptionsThrottleRequestsException) {\r\n        return response()-&gt;json(['message' =&gt; 'Too many requests, please slow down.'], 429);\r\n    }\r\n\r\n    return parent::render($request, $exception);\r\n}\r\n<\/code><\/pre><h3>&#27493;&#39588;5&#65306;&#27979;&#35797;&#24744;&#30340;API<\/h3><p>&#29616;&#22312;&#65292;&#20320;&#21487;&#20197;&#20351;&#29992;&#24037;&#20855;&#22914;Postman&#25110;cURL&#26469;&#27979;&#35797;API&#65292;&#20197;&#30830;&#20445;&#38480;&#27969;&#21151;&#33021;&#27491;&#24120;&#24037;&#20316;&#12290;&#21521;API&#31471;&#28857;&#21457;&#36865;&#35831;&#27714;&#24182;&#39564;&#35777;&#65292;&#22312;&#35774;&#23450;&#30340;&#38480;&#21046;&#21518;&#65292;API&#20250;&#36820;&#22238;&#29366;&#24577;&#30721;429&#12290;<\/p><p>&#35831;&#35760;&#20303;&#65292;&#36866;&#24403;&#30340;&#36895;&#29575;&#38480;&#21046;&#20540;&#21462;&#20915;&#20110;&#24744;&#30340;&#20855;&#20307;&#24212;&#29992;&#38656;&#27714;&#21644;&#26381;&#21153;&#22120;&#33021;&#21147;&#12290;&#26681;&#25454;&#38656;&#35201;&#36827;&#34892;&#30456;&#24212;&#30340;&#35843;&#25972;&#20197;&#23454;&#29616;&#26368;&#20339;&#24615;&#33021;&#21644;&#29992;&#25143;&#20307;&#39564;&#12290;<\/p><h2>&#32467;&#35770;&#12290;<\/h2><p>&#38480;&#27969;&#26159;&#19968;&#39033;&#33267;&#20851;&#37325;&#35201;&#30340;&#21151;&#33021;&#65292;&#29992;&#20110;&#30830;&#20445;&#24212;&#29992;&#31243;&#24207;&#30340;&#25345;&#20037;&#24615;&#21644;&#21487;&#38752;&#24615;&#12290;Laravel&#20869;&#32622;&#30340;&#38480;&#27969;&#26381;&#21153;&#25552;&#20379;&#20102;&#19968;&#20010;&#24378;&#22823;&#30340;&#19988;&#28789;&#27963;&#30340;&#31995;&#32479;&#12290;&#35760;&#20303;&#65292;&#27491;&#30830;&#23454;&#26045;&#38480;&#27969;&#30340;&#20851;&#38190;&#22312;&#20110;&#29702;&#35299;API&#30340;&#20351;&#29992;&#27169;&#24335;&#65292;&#24182;&#26681;&#25454;&#38656;&#35201;&#35843;&#25972;&#38480;&#21046;&#20197;&#20445;&#25345;&#21487;&#35775;&#38382;&#24615;&#21644;&#20445;&#25252;&#20043;&#38388;&#30340;&#24179;&#34913;&#12290;<\/p><p>&#35831;&#35760;&#20303;&#65292;&#36825;&#37324;&#20351;&#29992;&#30340;&#25216;&#26415;&#20063;&#21487;&#20197;&#36827;&#19968;&#27493;&#25193;&#23637;&#21644;&#23450;&#21046;&#20197;&#28385;&#36275;&#24744;&#30340;&#29305;&#23450;&#38656;&#27714;&#12290;&#38543;&#30528;&#24212;&#29992;&#31243;&#24207;&#30340;&#22686;&#38271;&#65292;&#35831;&#22987;&#32456;&#23457;&#26597;&#24182;&#35843;&#25972;&#36895;&#29575;&#38480;&#21046;&#35774;&#32622;&#65292;&#20197;&#30830;&#20445;&#26368;&#20339;&#24615;&#33021;&#21644;&#23433;&#20840;&#24615;&#12290;<\/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; Laravel &#20013;&#23454;&#29616;&#38480;&#27969;&#21151;&#33021;&#21487;&#20197;&#36890;&#36807;&#20351;&#29992; Eloquent ORM &#30340;&#32531;&#23384;&#26426;&#21046;&#26469;&#23454;&#29616;&#12290;&#20197;&#19979;&#26159;&#19968;&#20010;&#31616;&#21333;&#30340;&#20363;&#23376;&#65306;<\/p>\n<ol>\n<li>\n<p>&#39318;&#20808;&#65292;&#30830;&#20445;&#20320;&#24050;&#32463;&#23433;&#35013;&#20102; Eloquent ORM &#21644; Redis&#12290;<\/p>\n<\/li>\n<li>\n<p>&#22312;&#25511;&#21046;&#22120;&#20013;&#28155;&#21152;&#32531;&#23384;&#30340;&#38057;&#23376;&#65306;<\/p>\n<\/li>\n<\/ol>\n<pre><code class=\"language-php\">use IlluminateContractsCacheRepository;\nuse IlluminateHttpRequest;\n\npublic function __construct(Repository $cache)\n{\n    $this-&gt;cache = $cache;\n}\n\nprotected function cacheResponse(Response $response, Request $request)\n{\n    \/\/ &#32531;&#23384;&#21709;&#24212;\n    $response-&gt;headers-&gt;set('Cache-Control', 'max-age=600');\n    $this-&gt;cache-&gt;forever($request-&gt;fullUrl(), $response);\n}<\/code><\/pre>\n<ol start=\"3\">\n<li>&#20026;&#27599;&#20010;&#38656;&#35201;&#38480;&#27969;&#30340;&#26041;&#27861;&#28155;&#21152;&#32531;&#23384;&#38057;&#23376;&#65292;&#24182;&#35774;&#32622;&#30456;&#24212;&#30340;&#32531;&#23384;&#26102;&#38388;&#65288;&#20363;&#22914;&#65306;600 &#31186;&#65289;&#65306;<\/li>\n<\/ol>\n<pre><code class=\"language-php\">Route::get('\/api\/{endpoint}', [YourControllerName::class, 'yourMethod'])-&gt;middleware(['throttle:your_rate_limit'])-&gt;name('your_method');\n\n\/\/ &#31034;&#20363;&#65306;&#38480;&#27969;&#21040;&#27599;&#20998;&#38047; 5 &#27425;\nfunction yourMethod(Request $request) {\n    if ($request-&gt;has('limit')) {\n        return response()-&gt;json(['message' =&gt; 'Rate limit exceeded'], 429);\n    }\n    \/\/ &#20854;&#20182;&#19994;&#21153;&#36923;&#36753;...\n}<\/code><\/pre>\n<p>&#22312;&#36825;&#20010;&#20363;&#23376;&#20013;&#65292;<code>throttle:your_rate_limit<\/code> &#26159;&#38480;&#27969;&#30340;&#20013;&#38388;&#20214;&#65292;&#23427;&#20250;&#26816;&#26597;&#35831;&#27714;&#26159;&#21542;&#36798;&#21040;&#36895;&#29575;&#38480;&#21046;&#12290;&#22914;&#26524;&#36798;&#21040;&#20102;&#36895;&#29575;&#38480;&#21046;&#65292;&#21017;&#36820;&#22238;&#19968;&#20010; JSON &#21709;&#24212;&#34920;&#31034;&#36895;&#29575;&#38480;&#21046;&#24050;&#36229;&#26102;&#12290;<\/p>\n<p>&#27492;&#22806;&#65292;&#20026;&#20102;&#38450;&#27490;&#32531;&#23384;&#36807;&#26399;&#23548;&#33268;&#30340;&#24615;&#33021;&#38382;&#39064;&#65292;&#21487;&#20197;&#32771;&#34385;&#23558;&#32531;&#23384;&#26102;&#38388;&#35774;&#32622;&#24471;&#26356;&#38271;&#19968;&#20123;&#12290;&#20294;&#26159;&#35831;&#27880;&#24847;&#65292;&#32531;&#23384;&#26102;&#38388;&#36807;&#38271;&#21487;&#33021;&#23548;&#33268;&#25968;&#25454;&#24211;&#35775;&#38382;&#39057;&#32321;&#30340;&#38382;&#39064;&#12290;<\/p>\n<p>&#20197;&#19978;&#23601;&#26159;&#19968;&#20010;&#22522;&#26412;&#30340;&#38480;&#27969;&#23454;&#29616;&#65292;&#20320;&#21487;&#20197;&#26681;&#25454;&#20855;&#20307;&#38656;&#27714;&#35843;&#25972;&#36895;&#29575;&#38480;&#21046;&#21644;&#32531;&#23384;&#31574;&#30053;&#12290;<\/p>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Bug&#32534;&#35793;&#29422; &#22312; Laravel &#20013;&#23454;&#29616;&#38480;&#27969;&#65288;Rate Limiting&#65289;&#21487;&#20197;&#36890;&#36807;&#22810;&#038;#3118..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false},"tags":[],"my1js2nav":[44],"tuisongtax":[],"class_list":["post-757","my1js","type-my1js","status-publish","hentry","my1js2nav-laravel"],"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\/757","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=757"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tags?post=757"},{"taxonomy":"my1js2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js2nav?post=757"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tuisongtax?post=757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}