【问题标题】:Yii: common entry pointYii:常见的入口点
【发布时间】:2014-07-28 01:20:59
【问题描述】:

我想记录今天访问该网站的用户。为此,我必须在网站上的任何页面上处理用户访问。

什么是公共入口点(代码,在访问任何页面时执行)?

【问题讨论】:

  • index.php 是 .htaccess 指向的所有内容

标签: php yii statistics entry-point


【解决方案1】:

我想如果你想记录哪些用户访问了网站,那么你应该在user 组件中实现这个功能(默认为CWebUser)。您可以扩展此 calss 并在用户组件的配置中指定它:

'user'=>array(
    // enable cookie-based authentication
    'allowAutoLogin'=>true,
    'class'=>'MyWebUser',
),

【讨论】:

  • 好的,但是当用户从站点请求页面时,我该如何执行一些代码?
  • @DenisKulagin 例如如下:public function init(){ parent::init(); //your code here } 并在那里调用记录方法。
【解决方案2】:

您还可以创建类 BaseController 扩展 CController,并使用 init 方法。例如:



    class BaseController extends CController
    {
        public function init()
        {
            $this->loggedUserId    = Yii::app()->user->getId();
            $this->isLogged        = !empty($this->loggedUserId);

            if ($this->isLogged) {
                // some log actions
            }

            return parent::init();
        }
    }

【讨论】:

    【解决方案3】:

    假设你在谈论 Yii 1.1

    您可以在 onBeginRequestonEndRequest 事件上附加您的逻辑:

    示例(在适当的文件中,index.php/custom loader 脚本,简单)

    Yii::app()->onBeginRequest = function(CEvent $event) { handle_event($event); };
    

    或者在您的配置中将自定义行为附加到该事件:

    'behaviors' => array(
        'onbeginRequest' => array(
            'class' => 'application.components.AnalyticsBehaviour',
        )
    )
    

    并处理行为。

    【讨论】:

      猜你喜欢
      • 2013-10-21
      • 1970-01-01
      • 1970-01-01
      • 2012-08-27
      • 2016-08-17
      • 1970-01-01
      • 2016-02-19
      • 2023-04-09
      相关资源
      最近更新 更多