I have implemented both FOSUserBundle and HWIOauthBundle in my Symfony 2 project. Basically, everything works fine but I would like to customize a litle bit more. Typically I use the connect functionnality of the HWIOauthBundle to connect oauth account to user already connected with the authentication form (FOSUserBundle).
In case of succes, the controller action HWIOAuthBundle:Connect:connectService displays the twig template connect_success.html.twig. At this point, I would like to override this template and do the following actions :
You can obtain this behavior easily with FOSUserBundle as this bundle dispatchs many events to hook into the controllers. But with HWIOauthBundle this is not possible.
My solution is the following :
1/ I override connect_success.html.twig by placing the same named file in app/Ressources/HWIOauthBundle/views/Connect with the following code:
{{ render(controller('MyUserBundle:User:HWIOAuthFlash')) }}
2/ In my user controller (MyUserBundle:User), I create an action HWIOAuthFlashAction() that defines a flash message and forwards to the controller action that displays he homepage(MyMainBundle:Main:homepage)
public function HWIOAuthFlashAction()
{
// Here : flash message definition
return $this->forward('MyMainBundle:Main:homepage');
}
At this point the homepage is displayed with the flash message. But I had to remove two links in the homepage template (homepage.twig.html)that permits the user to switch between two locales.
The following code is the one that I had to remove from my template :
<ul>
<li><a href="{{ path( app.request.get('_route'), app.request.get( '_route_params')|merge({'_locale': 'fr'} )) }}">FR</a></li>
<li><a href="{{ path( app.request.get('_route'), app.request.get( '_route_params')|merge({'_locale': 'en'} )) }}">EN</a></li>
</ul>
I understand that that the special variable _route is null. And I get the following message :
An exception has been thrown during the rendering of a template ("Error when rendering "xxxxx/web/app_dev.php/connect/service/google?key=yyyyyyy" (Status code is 500).") in HWIOAuthBundle:Connect:connect_success.html.twig at line 3.
I have two questions :
As proposed in my comment, here is my solution :
In my homepage template homepage.twig.html :
{% set route = app.request.get('_route') ? app.request.get('_route') : 'homepage' %}
<ul>
<li><a href="{{ path(route, app.request.get( '_route_params')|merge({'_locale': 'fr'} )) }}">FR</a></li>
<li><a href="{{ path(route, app.request.get( '_route_params')|merge({'_locale': 'en'} )) }}">EN</a></li>
</ul>