Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何修改请求参数 #403

Open
douno23 opened this issue Mar 8, 2024 · 4 comments
Open

如何修改请求参数 #403

douno23 opened this issue Mar 8, 2024 · 4 comments

Comments

@douno23
Copy link

douno23 commented Mar 8, 2024

请问如何修改请求参数,并可以对后续别的方法起到影响
举例:
数据查询时,如果没有传递排序字段,就按照主键倒序排。 尝试过在 layui.table.render 方法中传递 where,但是点击搜索的时候不会传递这个
有没有办法,在控制器或者哪里修改参数,并且对后续产生影响

@qnnp-me
Copy link

qnnp-me commented May 3, 2024

你这是实际业务中的问题、和框架没关系、具体实现你可以在Controller中处理具体讲逻辑

@yisibugua
Copy link

yisibugua commented Oct 11, 2024

框架默认的查询是 select 方法,你重写掉就行了。

public function select(Request $request): Response
{
        [$where, $format, $limit, $field, $order] = $this->selectInput($request);
        // 这里判断$field,如果为空就给个默认值
        if (!$field) {
            $field = 'id';
            $order = 'desc';
        }
        $query = $this->doSelect($where, $field, $order);
        return $this->doFormat($query, $format, $limit);
}

@willvar
Copy link

willvar commented Nov 7, 2024

@keanu-jinu
Copy link

keanu-jinu commented Nov 12, 2024

重写项目根目录下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<[email protected]>
 * @copyright walkor<[email protected]>
 * @link      http://www.workerman.net/
 * @license   http://www.opensource.org/licenses/mit-license.php MIT License
 */

namespace support;

/**
 * Class Request
 * @package support
 */
class Request extends \Webman\Http\Request {
    /**
     * 获取过滤后的 post 数据
     * @param $name
     * @param $default
     * @return mixed|null
     */
    public function post($name = null, $default = null) {
        // 获取请求的所有参数
        $params = parent::post();  // 只获取 POST 请求的参数

        ...自定义处理逻辑

        if ($name) {
            return collect($params)->get($name, $default);
        } else {
            return $params;
        } 
    }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants