dongyanpai2701 2018-10-24 08:33
浏览 257
已采纳

使用urldecode预处理流明路径参数

I am currently using the lumen framework (5.6) to build an API, this API can be used to request a page by for example its title. The route for this is:

Route::group(["prefix" => '/api/v1', "middleware" => ["ContentTypeJson","Paginator"]], function () {


    Route::group(["prefix" => '/{databaseIdentifier}', "middleware"=>"DatabaseIdentifier"], function () {

        Route::group(["prefix" => '/pages'], function () {

            Route::group(["prefix" => '/{title}'], function () {

                Route::get("/", "PageController@getPageByTitle");

                Route::get("/parents", "SearchController@getParentalSpecies");

                Route::get("/all", "PageController@getPageByTitleWithLinks");

                Route::get("/overlap/{overlapProperty}", "PageController@getPagesWithOverlap");

                Route::put("/", "PageController@overwritePage");

            });

        });
});

As you can see the title is used in multiple functions and controllers, the same applies to the databaseIdentifier which is used in the middleware to determine which database needs to be used.

However all url parameters with a space will be converted with %20 instead of a space, which is the expected behaviour. However I would like to convert this back to the raw string, which can be done with urldecode(). But since this is applied in every controller and function I would like to use some kind of preprocessing step for this.

I have tried using a middleware for this to alter the route parameters as suggested here (using $request->route()->setParameter('key', $value);).

Unfortunately this does not work in lumen since the result of $request->route() is an array and not an object. I have tried altering this array but I can not get it to change the actual array in the Request object. No error appears here.

So in short: I am looking for a way to urldecode every URL parameter which is passed to my controllers and functions without putting $param = urldecode($param); everywhere.

If you need more information feel free to ask
Thank you in advance

  • 写回答

1条回答 默认 最新

  • dongyu6276 2018-10-24 09:35
    关注

    For anyone who also encounters this issue I have found a solution using middleware.

    In the middleware I do the following:

    public function handle(Request $request, Closure $next)
        {
            $routeParameters = $request->route(null)[2];
    
            foreach ($routeParameters as $key=>$routeParameter) {
                $routeParameters[$key] = urldecode($routeParameter);
            }
    
            $routeArray = $request->route();
            $routeArray[2] = $routeParameters;
            $request->setRouteResolver(function() use ($routeArray)
            {
                return $routeArray;
            });
    
            return $next($request);
        }
    

    This code will decode every route parameter and save it in an array, then I take the whole route array which is created by lumen itself (which contains the url encoded parameters), these are then replaced with the url decoded version of the parameter. This is not enough because this does not affect the route array in the Request object.

    In order to apply these changes I alter the routeResolver so it will return the changed array instead of the one created by lumen.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。