suncat2000 / MobileDetectBundle

Symfony 2/3/4 bundle for detect mobile devices, manage mobile view and redirect to the mobile and tablet version.
397 stars 154 forks source link

Query string gets lost on "device_view=XXX" #50

Closed tobias-r closed 10 years ago

tobias-r commented 10 years ago

Hi,

if I add device_view to my URL http://xxx.com/?param=1&device_view=full the bundle redirects to http://xxx.com/, so the query string gets lost.

Could you solve this issue?

Thank you! Tobi

wibimaster commented 10 years ago

To patch this, on RequestListener (I wrote another RequestListener that extends him to do this) :

/**
 * Gets the RedirectResponse by switch param.
 *
 * @return \Symfony\Component\HttpFoundation\RedirectResponse
 */
protected function getRedirectResponseBySwitchParam()
{
    if (true === $this->isFullPath) {
        $request = $this->container->get('request');
        $redirectUrl = $request->getUriForPath($request->getPathInfo());
        // Patch to keep queries
        $queries = array();
        parse_str ($request->server->get('QUERY_STRING'), $queries);
        if(array_key_exists('device_view', $queries)) {
            unset($queries['device_view']);
        }
        if(sizeof($queries) > 0) {
            $redirectUrl .= '?'.http_build_query($queries);
        }
        // End of patch
    } else {
        $redirectUrl = $this->getCurrentHost();
    }

    return $this->deviceView->getRedirectResponseBySwitchParam($redirectUrl);
}