Yii 登录时显示用户名和注销按钮 [英] Yii upon login show the username and logout button

查看:28
本文介绍了Yii 登录时显示用户名和注销按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户成功登录时,我想要一个注销按钮和我的标题小部件中的用户名.我已经创建了一个页眉和一个页脚小部件,不想再创建一个.我想要我可以在标题中获取用户名和注销按钮的代码.我也在使用 yii 权限和扩展.请在这件事上给予我帮助..!!我寻找答案,有人发布了这些东西.我不知道在哪里以及如何使用它.这是该解决方案的链接.

 user->isGuest){?><li><a href="<?php echo Yii::app()->request->baseUrl;?>/user/registration">注册</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 ?>) &nbsp;注销</a></li>

这是该解决方案的链接.显示注销按钮以及登录成功后的用户

 <div class="header"><div class="row logo"><div class="col-md-6"><br><a href=''><p>ERS.com</p></a>

<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-右:12px'>登录</p></a><?phpif (!Yii::app()->user->isGuest) {echo Yii::app()->user->name;}?>

</div><!--行在此结束--><div class="行导航"><br><div class="col-md-6 col-md-offset-3"><ul class="nav nav-pills"><li role="presentation"><a href="#"><p>首页</p></a></li><li role="presentation"><a href="#"><p>Ngo's</p></a></li><li role="presentation"><a href="#"><p>博客</p></a></li><li role="presentation"><a href="#"><p>故事</p></a></li><li role="presentation"><a href="#"><p>视频</p></a></li><li role="presentation"><a href="#"><p>画廊</p></a></li><br>

</div><!--行在此结束--><?phpif (!Yii::app()->user->isGuest) {echo Yii::app()->user->name;}?>

<!--标题在此结束--></div>-->

解决方案

你可以在你的布局中试试这个小部件

<div class="container" id="page"><div id="标题"><div id="logo"><?php echo CHtml::encode(Yii::app()->name);?></div></div><!-- 标题 --><div id="主菜单"><?php $this->widget('zii.widgets.CMenu',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'), '可见'=>!Yii::app()->user->isGuest)),));?></div><!-- 主菜单--><?php if(isset($this->breadcrumbs)):?><?php $this->widget('zii.widgets.CBreadcrumbs', array('链接'=>$this->面包屑,));?><!-- 面包屑 --><?php endif?><?php echo $content;?><div class="clear"></div><div id="页脚">版权&copy;<?php echo date('Y');?>由我的副本... - 保留所有权利.<br/><?php echo Yii::powered();?></div><!-- 页脚-->

最后两个是你要找的......

对于您的最后一个请求,我希望这就是您要查找的内容

user->isGuest) {回声 "<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>";}

?>

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 ?>) &nbsp;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>-->

解决方案

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 &copy; <?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>";
    }

?>

这篇关于Yii 登录时显示用户名和注销按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆