easy-swoole / easyswoole

swoole,easyswoole,swoole framework
https://www.easyswoole.com/
Apache License 2.0
4.75k stars 511 forks source link

Path parameter does not work for +v3.4.3 #473

Closed AhmedAsser closed 3 years ago

AhmedAsser commented 3 years ago

Hi, I noticed after upgrade to version v3.4.3 that path params not working anymore, if the route is for example "/packages/{i_package_id:\d+}" the request variable inside the controller does not have "i_package_id" params, after debugging the issue it appears that in the HTTP module the dispatcher class save the params in ContextManager, not like the old code

//working dispatcher.php $request->withQueryParams($vars+$data);

//not working dispatcher.php

 case AbstractRouter::PARSE_PARAMS_IN_CONTEXT:{
                                $vars = $routeInfo[2];
                                ContextManager::getInstance()->set(AbstractRouter::PARSE_PARAMS_CONTEXT_KEY,$vars);
                                break;
                            }

is it a bug or there is another way to let it work as usual?

Player626 commented 3 years ago

https://www.easyswoole.com/HttpServer/dynamicRoute.html#GET%E8%8E%B7%E5%8F%96%E8%B7%AF%E7%94%B1%E5%8F%82%E6%95%B0 You can set routing parameter injection by configuring items.

Player626 commented 3 years ago

If you are working as before, make the following settings.

class Router extends AbstractRouter
{
    function initialize(RouteCollector $routeCollector)
    {
        $this->parseParams(Router::PARSE_PARAMS_IN_GET);
        $routeCollector->get('/user/{id:\d+}', function (Request $request, Response $response) {
            $response->write(json_encode([
                'get' => $request->getQueryParams(), // 在这里可以获取 id 参数
                'post' => $request->getParsedBody(),
                'context' => ContextManager::getInstance()->get(Router::PARSE_PARAMS_CONTEXT_KEY)
            ]));
            return false;// 不再往下请求,结束此次响应
        });
        ## 访问: http://localhost:9501/user/100
        ## 响应结果: {"get":{"id":"100"},"post":[],"context":null}
    }
}

For more usage, take a look at the link to the document I posted above.