Thanks for the pointing out the parse_url function David; I felt
pretty iffy about all that string replacement. This is much cleaner
(and is a great function to know!)

I still had to strip the controller out as a seperate step, but that
is cool. The new action looks like:

    public function switchLangAction($_locale)
    {
        $newLang = ($_locale == "en") ? "fr" : "en";

        $request = $this->get('request');
        $router = $this->get('router');
        $domain = $request->server->get('HTTP_HOST');

        $referrer = $request->server->get('HTTP_REFERER');
        // pluck out the URI from the URL
        $referrer = parse_url($referrer, PHP_URL_PATH);
        // remove the controller
        $referrer = str_replace($request->getBaseUrl(), '',
$referrer);
        // match the resulting cleaned up route
        $referrerParams = $router->match( $referrer );
        // pluck the _route out of the params
        $referrerRoute = $referrerParams['_route'];
        unset($referrerParams['_route']);
        // switch the locale
        $referrerParams['_locale'] = $newLang;

        $response = new RedirectResponse( $this->generateUrl(
            $referrerRoute,
            $referrerParams
        ));
        return $response;
    }

On May 10, 1:56 pm, David Buchmann <david.buchm...@liip.ch> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> hi
>
> i think you should generate the current route with all locales you
> support and directly use those to switch the language, instead of having
> the client send a request and then redirecting.
>
> as a side note, instead of the string replacement, you could use the php
> method parse_url to get the elements from an url.
>
> cheers,
> david
>
> Am 10.05.2011 17:56, schrieb theinterned:
>
>
>
>
>
>
>
>
>
> > I solved this.
>
> > I found the $router->match() method, which I am using against a
> > cleaned up version of the $request->server->get('HTTP_REFERER'). This
> > gives me the route parameters I need to generate my redirect route.
>
> > Here is the completed action:
>
> >     public function switchLangAction($_locale)
> >     {
> >         $newLang = ($_locale == "en") ? "fr" : "en";
>
> >         $request = $this->get('request');
> >         $router = $this->get('router');
> >         $domain = $request->server->get('HTTP_HOST');
> >         $referrer = $request->server->get('HTTP_REFERER');
>
> >         // remove the protocol
> >         $referrer = preg_replace(';\b((ftp|https?)://)+?\b;', '',
> > $referrer);
> >         // remove the domain
> >         $referrer = str_replace($domain, '', $referrer );
> >         // remove the controller
> >         $referrer = str_replace($request->getBaseUrl(), '',
> > $referrer);
> >         // remove the query string
> >         $referrer = preg_replace('/\?.*/', '', $referrer);
> >         // match the resulting cleaned up route
> >         $referrerParams = $router->match( $referrer );
> >         // pluck the _route out of the params
> >         $referrerRoute = $referrerParams['_route'];
> >         unset($referrerParams['_route']);
> >         // switch the locale
> >         $referrerParams['_locale'] = $newLang;
>
> >         $response = new RedirectResponse( $this->generateUrl(
> >             $referrerRoute,
> >             $referrerParams
> >         ));
> >         return $response;
> >     }
>
> > On May 9, 1:01 pm, Matt <matthieu.o.vac...@gmail.com> wrote:
> >> Hi,
>
> >> This is how I did it:
>
> >> public function changeLocaleAction($locale)
> >> {
> >>     if ($this->request->hasSession())
> >>         $this->session->setLocale($locale);
>
> >>     $referer = $this->request->server->get('HTTP_REFERER');
> >>     if (!isset($referer))
> >>         $referer = $this->generateUrl('index');
>
> >>     return new RedirectResponse($referer);
>
> >> }
>
> >> I'm not sure if it is the best to do it way but it works for me in my use
> >> cases.
>
> >> Regards,
> >> Matt
>
> - --
> Liip AG // Agile Web Development // T +41 26 422 25 11
> CH-1700 Fribourg // PGP 0xA581808B //www.liip.ch
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.10 (GNU/Linux)
> Comment: Using GnuPG with Mozilla -http://enigmail.mozdev.org/
>
> iEYEARECAAYFAk3JfDgACgkQqBnXnqWBgIuDHwCfVp25iEopMHTigFm5Q8AQmzM0
> NdwAn2hCK2FCW+T/xbj118tgjHnzNqPA
> =E/QL
> -----END PGP SIGNATURE-----

-- 
If you want to report a vulnerability issue on symfony, please send it to 
security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "symfony users" group.
To post to this group, send email to symfony-users@googlegroups.com
To unsubscribe from this group, send email to
symfony-users+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/symfony-users?hl=en

Reply via email to