kjmt's avatar
Level 1

dealing with percent-encodings in URLs

I have a route that looks like this:

$app->get('/query/{name}', ....

And it works fine for this URL: http://mysite.com/query/o'connor

But fails to match for this URL http://mysite.com/query/o%27connor

It looks like the %27, which is percent-encoded just gets passthrough literally and is not treated like a single quote.

How can I get Lumen to handle percent-encoded URLs?

Thanks.

0 likes
2 replies
bobbybouwmann's avatar

I think you can do this in your controller

public function get($string)
{
    $string = preg_replace("%27", "/'/", $string);
}
gdecris's avatar

One way you can do it is to extend the Application and then re-write the getPathInfo method to decode the uri.

<?php namespace App\MyApplication;

use Laravel\Lumen\Application;

class ApiApplication extends Application {

    /**
     * Get the current HTTP path info.
     *
     * @return string
     */
    public function getPathInfo()
    {
        $query = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : '';

        return '/'.trim(str_replace('?'.$query, '', rawurldecode($_SERVER['REQUEST_URI'])), '/');
    }
}

Please or to participate in this conversation.