天天看點

Yii項目開發總結

最近使用Yii架構給公司開發了兩個小型項目,積累了一些知識,在此進行彙總一下,以便将來項目用到時,可以參考。

關于使用者登入登出功能。

CWebUser class,使用這個元件能輕松實作使用者登入,需要個性化設定session變量時,可以建立一個類繼承CWebUser 且重寫 login()方法,比如:

public function login($identity, $duration=0) 

  {     

        $this->setState('__userInfo', array('__id'=>$identity->getId(),'__name'=>$identity->getName(),'__firstName'=>$identity->getFirstName()));

        parent::login($identity, $duration);  

  }

登出時,如果不希望将該應用的所有session都删除,可以這樣寫Yii::app()->user->logout(false);

 登入時,需要驗證使用者身份,Yii中使用UserIdentity類來處理該邏輯。參考代碼如下:

public function Authenticate()

    {

        $member = TMember::model()->findByAttributes(array('EMAIL'=>$this->username,'PASSWORD'=>md5($this->password),'IS_ACTIVED'=>1));

                if($member)

                {                                          

                        $this->setId($member->ID);

                        $this->setName($member->EMAIL);      

                        $this->setFirstName($member->FIRST_NAME);

                        $this->setPassword($this->password);

                        $this->errorCode=self::ERROR_NONE;

                }else{

                    $rs1 = TMember::model()->findByAttributes(array('EMAIL'=>$this->username));

                    $rs2 = TMember::model()->findByAttributes(array('PASSWORD'=>$this->password));

                    $rs3 = TMember::model()->findByAttributes(array('EMAIL'=>$this->username,'PASSWORD'=>md5($this->password)));

                     if(!$rs3){

                    if(!$rs1){

                        $this->errorCode=self::ERROR_USERNAME_INVALID;

                    }else{

                        if(!$rs2){

                          $this->errorCode=self::ERROR_PASSWORD_INVALID;  

                        }

                      }                        

                        $this->errorCode=self::ERROR_IS_NOT_ACTIVED;

                    }

                }

                 $this->tErrorCode['front'] = $this->errorCode;

                return !$this->tErrorCode['front'];

    }

2.  采用cookie以及session實作跨域登入

   首先,要搭建好環境,比如設定虛拟域名。

   a. 首先修改C:\Windows\System32\drivers\etc目錄下的 hosts 檔案,用記事本打開,加入:

      127.0.0.1 mc.meztalks.com 127.0.0.1 meztalks.com記得去掉前面的#

     127.0.0.1 mc.meztalks.com

     127.0.0.1 meztalks.com

     127.0.0.1 test.meztalks.com

步驟閱讀

Yii項目開發總結

b.打開xampp\apache\conf\httpd.conf檔案,搜尋 “Include conf/extra/httpd-vhosts.conf”,確定前面沒有 # 注釋符,也就是確定引入了 vhosts 虛拟主機配置檔案。效果如下:

# Virtual hosts

Include "conf/extra/httpd-vhosts.conf"

開啟了httpd-vhosts.conf,預設a的httpd.conf預設配置失效(確定 httpd-vhosts.conf 檔案裡也開啟了虛拟主機配置,見第3條),通路此IP的域名将全部指向 vhosts.conf 中的第一個虛拟主機。

c. 定位apache安裝目錄,如:D:\wamp\bin\apache\apache2.4.9\conf\extra,編輯httpd-vhosts.conf, 設定想要配置的項目目錄,

<VirtualHost *:80>

    ServerAdmin [email protected]

    DocumentRoot "E:/eztalks/eztalks-com/"

    ServerName meztalks.com

#ErrorLog "logs/dummy-host2.example.com-error.log"

#CustomLog "logs/dummy-host2.example.com-access.log" common

</VirtualHost>

    DocumentRoot "E:/eztalks/meetingcenter/"

    ServerName mc.meztalks.com

    ServerAlias mc.meztalks.com

DirectoryIndex index.php index.html index.htm

   #ErrorLog "logs/dummy-host.example.com-error.log"

   #CustomLog "logs/dummy-host.example.com-access.log" common

虛拟域名到此已經配置好了。接下來可以進入主題。

cookie可以跨二級域名通路,但session不能跨域通路,是以想要實作單點登入,可以嘗試的方案有:

采用cookie傳session_id,采用資料庫儲存session變量。

cookie跨二級域名通路,很容易實作,隻要設定domain即可,參考代碼:

'session' => array (

              'class' => 'application.components.MCCDbHttpSession',

                                     'cookieParams'=>array('domain'=>'.meztalks.com','lifetime'=>0,'path'=>'/'),

                    'connectionID' => 'db',

                    'sessionTableName' => 't_db_session',

                ),

采用資料庫儲存session,參考Yii自帶的CDbHttpSession類,有特許需要,可以重寫該類繼承CDbHttpSession,重寫openSession(),readSession(),writeSession(),destroySession().

繼續閱讀