Jonathon Posted January 22, 2012 Share Posted January 22, 2012 Hi Larry, I have a quick (hopefully) question for you, what is the best way to perform dynamic login/logout links with Yii? I.e once you're logged in show "Logout User's name" but if you're not logged in show "Login". Currently I have this code inside my main.php 'id'=>('menu'), 'items'=>array( /* Menu links go here*/ array('label'=>'Create Account', 'url'=>array('/site/contact'), 'itemOptions'=> array('id'=>'sign_up'), 'active'=>false), array('label'=>'Sign in', 'url'=>array('/site/login'), 'itemOptions'=> array('id'=>'sign_in'), 'active'=>false, 'visible'=>Yii::app()->user->isGuest), array('label'=>'Logout ('.Yii::app()->user->firstName.')', 'url'=>array('/site/logout'), 'itemOptions'=> array('id'=>'sign_in'), 'active'=>false, 'visible'=>!Yii::app()->user->isGuest) As you can see I am showing the sign in link if the user->isGuest. However if the user is not a guest I show the logout link. My problem is that if I am not logged in this fails because Yii::app()->user->firstName doesn't exist as I set this (setState) at login. What is the best way to achieve personalised dynamic login/logout links within Yii? Thanks Jonathon Link to comment Share on other sites More sharing options...
Larry Posted January 24, 2012 Share Posted January 24, 2012 You could use the ternary operator inline to only add Yii::app()->user->firstName if it's set or if the user isn't a guest. 1 Link to comment Share on other sites More sharing options...
Jonathon Posted January 26, 2012 Author Share Posted January 26, 2012 Thanks Larry, never thought of that Link to comment Share on other sites More sharing options...
Recommended Posts