问题
i want a logout button and the username in my header widget when the user logs into successfully. i have already created a header and a footer widget and dont want to create another one. i want the code by which i can get the username and logout button in the header. i am using yii rights and extensions too. please help me with this..!! i looked for the answer and someone had posted this stuff. i dont know where and how to use it. and this is the link of that solution.
<?php if(Yii::app()->user->isGuest){?>
<li><a href="<?php echo Yii::app()->request->baseUrl;?>/user /registration">Sign up</a></li>
<li><a href="<?php echo Yii::app()->request->baseUrl;?>/user /login">login</a></li> <?php }?>
<?php if(!Yii::app()->user->isGuest){?>
<li><a href="<?php echo Yii::app()->request->baseUrl;?>/user/logout">
(<?php echo Yii::app()->user->name ?>) logout</a></li>
and this is the link of that solution. show logout button along with the name of user after successfull login
<!--header.php-->
<div class="header">
<div class="row logo">
<div class="col-md-6">
<br>
<a href=''><p>ERS.com</p></a>
</div>
<div class="col-md-6">
<br>
<a href=''><p class='pull- right'>SignUp</p></a>
<a href='<?php echo Yii::app()->request->baseUrl;?>/site/login'><p class='pull-right' style='margin-right: 12px'>LogIn</p></a>
<?php
if (!Yii::app()->user->isGuest) {
echo Yii::app()->user->name;
}
?>
</div>
</div><!--row ending here-->
<div class="row navigation">
<br>
<div class="col-md-6 col-md-offset-3">
<ul class="nav nav-pills">
<li role="presentation"><a href="#"> <p>Home</p> </a></li>
<li role="presentation"><a href="#"> <p>Ngo's</p> </a></li>
<li role="presentation"><a href="#"> <p>Blogs</p> </a></li>
<li role="presentation"><a href="#"> <p>Stories</p> </a></li>
<li role="presentation"><a href="#"> <p>Videos</p> </a></li>
<li role="presentation"><a href="#"> <p>Gallery</p> </a></li>
</ul>
<br>
</div>
</div><!--row ending here-->
<?php
if (!Yii::app()->user->isGuest) {
echo Yii::app()->user->name;
}
?>
</div> <!--header ending here-->
</div>-->
回答1:
You can try this widget in your layout
<body>
<div class="container" id="page">
<div id="header">
<div id="logo"><?php echo CHtml::encode(Yii::app()->name); ?></div>
</div><!-- header -->
<div id="mainmenu">
<?php $this->widget('zii.widgets.CMenu',array(
'items'=>array(
array('label'=>'Home', 'url'=>array('/site/index')),
array('label'=>'About', 'url'=>array('/site/page', 'view'=>'about')),
array('label'=>'Contact', 'url'=>array('/site/contact')),
array('label'=>'Login', 'url'=>array('/site/login'), 'visible'=>Yii::app()->user->isGuest),
array('label'=>'Logout ('.Yii::app()->user->name.')', 'url'=>array('/site/logout'), 'visible'=>!Yii::app()->user->isGuest)
),
)); ?>
</div><!-- mainmenu -->
<?php if(isset($this->breadcrumbs)):?>
<?php $this->widget('zii.widgets.CBreadcrumbs', array(
'links'=>$this->breadcrumbs,
)); ?><!-- breadcrumbs -->
<?php endif?>
<?php echo $content; ?>
<div class="clear"></div>
<div id="footer">
Copyright © <?php echo date('Y'); ?> by my copy... - All Rights Reserved.<br/>
<?php echo Yii::powered(); ?>
</div><!-- footer -->
the last two are what you are looking for ...
And for your last request i hope this is what you are looking for
<?php
if (Yii::app()->user->isGuest) {
echo "<a href='" . Yii::app()->request->baseUrl .
"/site/login'><p class='pull-right' style='margin-right: 12px'>LogIn</p></a>";
}
........
.....
if (!Yii::app()->user->isGuest) {
echo Yii::app()->user->name . " <a href='" .
Yii::app()->request->baseUrl .
"/site/logout'><p class='pull-right' style='margin-right: 12px'>Logout</p></a>";
}
?>
来源:https://stackoverflow.com/questions/33597530/yii-upon-login-show-the-username-and-logout-button