免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2423 | 回复: 0
打印 上一主题 下一主题

Yii框架整合Ucenter更新与增强 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-09-30 13:03 |只看该作者 |倒序浏览
Yii框架整合Ucenter更新与增强



在我前面的博文中提出了整合ucenter到yii应用的方法,还有一些不完美,那就是,登录、退出需要手动输出js到页面上来通知其他应用。那么如何做到自动处理,而不需要特别照顾?我发现只需要继承CWebUser类,实现自己的WebUser类,并覆盖登录和退出两个事件即可,不多说,上代码:



Php代码

  1. <?php
  2. class WebUser extends CWebUser
  3. {
  4.     public function afterLogin($fromCookie)
  5.     {
  6.                 parent::afterLogin ( $fromCookie );
  7.                
  8.                 //ucenter
  9.                 Yii::import ( 'application.vendors.*' );
  10.                 include_once 'ucenter.php';
  11.                
  12.                 $script = uc_user_synlogin ( $this->getId () );
  13.                 $count = preg_match_all ( '/src="(.+?)"/i', $script, $matches );
  14.                
  15.                 if ($count > 0) {
  16.                         foreach ( $matches [1] as $file ) {
  17.                                 Yii::app ()->clientScript->registerScriptFile ( $file, CClientScript::POS_END );
  18.                         }
  19.                 }
  20.                 //局部刷新顶部登录状态
  21.                 Yii::app()->clientScript->registerScript('refresh-login-status', 'top.$("#top_nav").load("'.CHtml::normalizeUrl(array('/site/login_status')).'");');
  22.         }
  23.        
  24.         public function afterLogout()
  25.         {
  26.                 parent::afterLogout();
  27.                 //ucenter
  28.                 Yii::import ( 'application.vendors.*' );
  29.                 include_once 'ucenter.php';
  30.                
  31.                 $script = uc_user_synlogout();
  32.                 $count = preg_match_all ( '/src="(.+?)"/i', $script, $matches );
  33.                
  34.                 if ($count > 0) {
  35.                         foreach ( $matches [1] as $file ) {
  36.                                 Yii::app ()->clientScript->registerScriptFile ( $file, CClientScript::POS_END );
  37.                         }
  38.                 }
  39.                 Yii::app()->clientScript->registerScript('refresh-login-status', 'top.$("#top_nav").load("'.CHtml::normalizeUrl(array('/site/login_status')).'");');
  40.         }
  41. }
复制代码
可以看到,我用正则匹配出了各应用通知的js,然后用CClientScript注册到页面底部。另外,由于在我的应用中头部有个局部加载的登录状态,用jquery自动更新了。



将这个类放在components目录下,接下来修改config/main.php的user段设置即可:



Php代码

  1. 'user'=>array(
  2.                         'class'=>'WebUser',
  3.                         // enable cookie-based authentication
  4.                         'allowAutoLogin'=>true,
  5.                         'loginUrl' => array('/site/login'),
  6.                 ),
复制代码
这样有个最大的好处,就是应用记住用户登录后,下次用户访问网站任何页面,都会自动登录,并同时登录到其他应用。但要注意:在用户登录成功、退出成功等action中,不要直接redirect,还是需要输出一个跳转页面,否则这些js不会输出到浏览器。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP