swoft-cloud / swoft

🚀 PHP Microservice Full Coroutine Framework
https://swoft.org
Apache License 2.0
5.58k stars 788 forks source link

swoft适配easywechat 添加2个方法 #518

Closed daviscai closed 5 years ago

daviscai commented 6 years ago

使用swoft开发微信公众号过程中发现swoft不支持easywechat使用的方法,下面代码亲测OK,希望官方支持下

/swoft/vendor/swoft/http-message/src/Server/Request.php

添加如下2个方法即可支持

    /**
     * Retrieve raw body from the request
     *
     * @return array|string|mixed
     */
    public function getContent()
    {
        $body = $this->getBody();
        $raw = '';
        if ($body instanceof SwooleStream) {
            $raw = $body->getContents();
        }
        return $raw;
    }

    /**
     * Retrieve query string arguments.
     * 
     * @param string $param the query string.
     * @return string
     */
    public function get($param) 
    {
        return $this->query($param);
    }
huangzhhui commented 6 years ago

@daviscai 可以直接提交一个PR到 swoft-component

limingxinleo commented 6 years ago

其实我不是很赞成这么来改。。

@daviscai 你是打算直接使用Request代替easywechat里的Request直接注入到easywechat里么?如果是这样,你其实只是想解决request初始化的问题。

所以你可以主动初始化一下即可。

如下

        $get = $request->getSwooleRequest()->get ?? [];
        $post = $request->getSwooleRequest()->post ?? [];
        $attr = [];
        $cookies = $request->getSwooleRequest()->cookie ?? [];
        $files = $request->getSwooleRequest()->files ?? [];
        $server = $request->getSwooleRequest()->server ?? [];
        $raw = $request->getSwooleRequest()->rawContent();

        // 初始化 Request
        $app->request->initialize($get, $post, $attr, $cookies, $files, $server, $raw);
daviscai commented 6 years ago

@limingxinleo 你的方法可以解决问题,不过 easywechat的Request是通用的 Symfony\Component\HttpFoundation\Request 对象 , 而swoft Request 没有继承它的常用方法,其他工具类或者框架在swoft下可能会有问题,需要做适配,我觉得swoft Request 应该支持 Symfony Request 大部分常用方法,对开发者会更友好和降低学习成本

limingxinleo commented 6 years ago

@daviscai 其实,这种情况可以基于Swoft Request再进行封装实现 Symfony Request的常用方法。

如果现在是Symfony Request,后面又有一个Symfony2 Request,Swoft Request肯定不能满足所有的效果。不过如果Psr里有RequestInterface的规范,Swoft肯定会去实现的。。

这里还希望你能谅解

huangzhhui commented 6 years ago

get() 方法作为一个 query() 方法的别名这个我觉得可以有,但是 getContent() 别名 raw() 我是觉得有点奇怪的

stelin commented 5 years ago

建议升级到2.0, 2.0 已经正式发布