Reputation: 2675
In my web app if a user is logged in and clicks on the logo, instead of going to the entry page as an anonymous user, I would load their member homepage URL instead.
How it works is when a user joins or logs in, I generate this route $this->generateUrl('member_page'), array('member' => $member->getName()));
and my route is configured as so,
member_page: pattern: /member/{member}/
defaults: { _controller: HomeBundle:Default:member }
and it generates this url:
http://website.com/member/John+joe/
The problem is, when I recall the route it just shows this url
http://website.com/member/
I have tried calling a new action and using $request->getURI()
but it does not maintain the dynamic get parameters.
I'm trying to avoid a call to the DB to get the user name each time the click the logo. Any help would be great, thanks!
Upvotes: 1
Views: 8533
Reputation: 799
A few weeks ago, I had to do something quite similar and inspired myself from the PagerFantaBundle. Have you tried this code in your controller:
$request = $this->getRequest();
return $this->redirect($this->generateUrl($request->get('_route'), $request->query->all()));
Upvotes: 4
Reputation: 17976
One of possible solutions would be to generate proper routes right in Twig:
{% if app.user %}
<a href="{{ path('member_page', {'member': app.user.name}) }}">
{% else %}
<a href="{{ path('your_homepage') }}">
{% endif %}
<img src="yourlogo.png" />
</a>
Upvotes: 0
Reputation: 2118
Instead of using the username you could use the user ID which will be saved into the users session after log in. This will also avoid any conflicts with users who have the same name.
Upvotes: 2