国产+高潮+在线,国产 av 仑乱内谢,www国产亚洲精品久久,51国产偷自视频区视频,成人午夜精品网站在线观看

使用 setGet 修改了參數(shù), 參數(shù)是拼接上去的, 頻繁訪問(wèn)的話(huà), 參數(shù)會(huì)一直拼接

debm

問(wèn)題描述

業(yè)務(wù)需要, 將傳過(guò)來(lái)的用戶(hù)名, 加一個(gè)前綴保存, 但是頻繁訪問(wèn)的話(huà), 這個(gè)前綴會(huì)一直加.
比如傳的 username 是 test 前綴是 default_, 如果頻繁訪問(wèn), 這個(gè) username 會(huì)變成 default_default_test 前綴會(huì)一直往前面加...是我寫(xiě)法有問(wèn)題, 還是用的不對(duì)?

這是運(yùn)行環(huán)境

-------------------------------------------- WORKERMAN ---------------------------------------------
Workerman/5.1.0 PHP/8.4.4 (Jit off) Darwin/23.6.0

$payload = $request->get();

$prefix = 'default_';
foreach ($payload as $key => $value) {
    if ('username' == $key) {
       $payload[$key] = $prefix . $value;
    }
}

$request->setGet($payload);

補(bǔ)充一下: 上面的修改代碼是在中間件里寫(xiě)的, 使用這個(gè)參數(shù)的時(shí)候是在控制器里.

477 3 1
3個(gè)回答

Noah
if ('username' == $key) {
    // 如果值不以 $prefix 開(kāi)頭,才添加前綴
    if (strpos($value, $prefix) !== 0) {
        $payload[$key] = $prefix . $value;
    }
}
  • debm 2025-03-06

    如果前端傳的就是這個(gè)名字呢? 比如前綴寫(xiě)成 abc. 前端傳的名字是 abcdefg , 這樣也是要加前綴的.

  • Noah 2025-03-06

    這就是你業(yè)務(wù)要規(guī)避的。你前綴可以設(shè)置個(gè)_開(kāi)始啊。用戶(hù)名肯定不能_開(kāi)始的不就行了。反正讓 prefix 和 username 互斥。

  • debm 2025-03-06

    先不說(shuō)業(yè)務(wù)是不是要規(guī)避, 單就這個(gè)問(wèn)題本身來(lái)說(shuō), 我感覺(jué)不對(duì).

  • Noah 2025-03-06

    我覺(jué)得最簡(jiǎn)單的方法是 prefix 和 username 互斥,因?yàn)橛么a去處理這種邊界情況相對(duì)來(lái)說(shuō)比較麻煩。誰(shuí)知道用戶(hù)名會(huì)注冊(cè)各種奇怪的名字。為了幾個(gè)特殊用戶(hù)的名字去用代碼完善劃不來(lái)。

胡桃

復(fù)用 Request 對(duì)象時(shí)沒(méi)有清理臟數(shù)據(jù)。

  • debm 2025-03-06

    這怎么清? 我看文檔上沒(méi)有, 看了這個(gè) Webman\Http\Request 代碼, 看著也沒(méi)有對(duì)應(yīng)的方法.

  • 胡桃 2025-03-06

    support/Request.php

    <?php
    /**
     * This file is part of webman.
     *
     * Licensed under The MIT License
     * For full copyright and license information, please see the MIT-LICENSE.txt
     * Redistributions of files must retain the above copyright notice.
     *
     * @author    walkor<walkor@workerman.net>
     * @copyright walkor<walkor@workerman.net>
     * @link      http://www.wtbis.cn/
     * @license   http://www.opensource.org/licenses/mit-license.php MIT License
     */
    
    namespace support;
    
    /**
     * Class Request
     *
     * @package support
     */
    class Request extends \Webman\Http\Request
    {
        public function clearGetData(): void
        {
            if (isset($this->data['get'])) {
                unset($this->data['get']);
            }
        }
    }

    app/middleware/ClearGetData.php

    <?php
    declare(strict_types=1);
    
    namespace app\middleware;
    
    use Webman\Http\Request;
    use Webman\Http\Response;
    use Webman\MiddlewareInterface;
    
    class ClearGetData implements MiddlewareInterface
    {
        public function process(Request $request, callable $handler): Response
        {
            // TODO: Implement process() method.
            $request->clearGetData();
            return $handler($request);
        }
    }

    app/config/middleware.php

    <?php
    /**
     * This file is part of webman.
     *
     * Licensed under The MIT License
     * For full copyright and license information, please see the MIT-LICENSE.txt
     * Redistributions of files must retain the above copyright notice.
     *
     * @author    walkor<walkor@workerman.net>
     * @copyright walkor<walkor@workerman.net>
     * @link      http://www.wtbis.cn/
     * @license   http://www.opensource.org/licenses/mit-license.php MIT License
     */
    
    return [
        '' => [
            \app\middleware\ClearGetData::class,
        ],
    ];
  • debm 2025-03-06

    感謝, 我試了下, 可以這樣處理.

damao
<?php
namespace app\middleware;
use Webman\MiddlewareInterface;
use Webman\Http\Response;
use Webman\Http\Request;
class Test implements MiddlewareInterface
{
    public function process(Request $request, callable $handler) : Response
    {
        $payload = $request->get();
        $prefix = 'default_';
        foreach ($payload as $key => $value) {
            if ('username' == $key) {
                $payload[$key] = $prefix . $value;
            }
        }
        $request->setGet($payload);
        return $handler($request);
    }
}
<?php
return [
    '' => [
        \app\middleware\Test::class
    ]
];
<?php
namespace app\controller;
use support\Request;
class IndexController
{
    public function index(Request $request)
    {
        return json($request->get());
    }
}

截圖

刷新了N次也沒(méi)復(fù)現(xiàn)

  • debm 2025-03-06

    不知道是不是我們安裝的環(huán)境和版本不一樣的原因, 我這里一直刷一直有...

??