I know I can set postLoginRedirect
in config/general.php to send the user to a specific page after login. Is there a way to grab that value in the template? This came up because I noticed that the {{siteUrl}}
href in the default _layout.html
template was sending the user to the homepage (index.html), but I'd like to conditionally replace that link for a logged-in user.
Add a comment
|
1 Answer
Sure, something like this should work (untested):
<a href="{{ currentUser ? url(craft.config.get('postLoginRedirect')) : siteUrl }}">Home</a>
The above should create an absolute URL to whatever's in your postLoginRedirect
config setting, relative to your siteUrl
.
Note that if you want an URL relative to your Control Panel (e.g. "dashboard"), you'll want to use cpUrl()
instead, i.e.
<a href="{{ currentUser ? cpUrl(craft.config.get('postLoginRedirect')) : siteUrl }}">Home</a>
-
Just noticed that with 'postLoginRedirect' => 'dashboard' using the solution above makes the postLoginRedirect a relative link, so it doesn't work on sub-pages. Can you think of a solution besides hard coding an absolute link like 'postLoginRedirect' => '/dashboard'– KeithCommented Feb 17, 2016 at 23:16
-
@Keith You're correct – just updated the answer with a solution. Commented Feb 17, 2016 at 23:22