CakePHP的會(huì)話管理

2018-01-19 15:06 更新

會(huì)話允許我們通過請(qǐng)求來(lái)管理唯一的用戶,并為特定用戶存儲(chǔ)數(shù)據(jù)。會(huì)話數(shù)據(jù)在任何可訪問組件的地方都可以得到,即可從控制器,視圖,助手類,元類和組件訪問。

訪問會(huì)話對(duì)象

會(huì)話對(duì)象可以通過執(zhí)行下面的代碼來(lái)創(chuàng)建。

$session = $this->request->session();

寫入會(huì)話數(shù)據(jù)

在會(huì)話中寫入數(shù)據(jù),我們可以使用write()方法 。

Session::write($key, $value)

上述方法有兩個(gè)參數(shù):和該鍵下存儲(chǔ)的 。

$session->write('name', 'Virat Gandhi');

讀取會(huì)話數(shù)據(jù)

若要從會(huì)話中檢索存儲(chǔ)的數(shù)據(jù),我們可以使用read()方法 。

Session::read($key)

上述方法只有一個(gè)參數(shù),就是在寫入數(shù)據(jù)時(shí)使用的鍵 。一旦正確的提供了正確的鍵,那么函數(shù)將返回其值。

$session->read('name');

當(dāng)您要檢查會(huì)話是否存在特定的數(shù)據(jù),那么你可以使用check()方法 。

Session::check($key)

上述方法只需要一個(gè)參數(shù):鍵。

if ($session->check('name')) {
   // name exists and is not null.
}

刪除會(huì)話數(shù)據(jù)

若要從會(huì)話中刪除數(shù)據(jù),我們可以使用delete()方法刪除數(shù)據(jù)。

Session::delete($key)

上述方法只有一個(gè)參數(shù):需要?jiǎng)h除的值的鍵。

$session->delete('name');

當(dāng)你想先讀取數(shù)據(jù),然后刪除數(shù)據(jù)的話,我們就可以使用consume()方法 。

static Session::consume($key)

上述方法只有一個(gè)參數(shù):鍵。

$session->consume('name'); 

銷毀會(huì)話

當(dāng)用戶從網(wǎng)站登出時(shí),我們需要銷毀會(huì)話,可以使用destroy()方法。

Session::destroy()

$session->destroy();

銷毀會(huì)話將刪除服務(wù)器中的所有會(huì)話數(shù)據(jù),但不會(huì)刪除會(huì)話cookie。

更新會(huì)話

若要續(xù)訂用戶會(huì)話,我們就可以使用renew()方法。

Session::renew()

$session->renew();

完整的會(huì)話

修改config/routes.php文件如下:
config/routes.php
<?php
   use CakeCorePlugin;
   use CakeRoutingRouteBuilder;
   use CakeRoutingRouter;

   Router::defaultRouteClass('DashedRoute');
   Router::scope('/', function (RouteBuilder $routes) {
      $routes->connect('/sessionobject',
         ['controller'=>'Sessions','action'=>'index']);
      $routes->connect('/sessionread',
         ['controller'=>'Sessions','action'=>'retrieve_session_data']);
      $routes->connect('/sessionwrite',
         ['controller'=>'Sessions','action'=>'write_session_data']);
      $routes->connect('/sessioncheck',
         ['controller'=>'Sessions','action'=>'check_session_data']);
      $routes->connect('/sessiondelete',
         ['controller'=>'Sessions','action'=>'delete_session_data']);
      $routes->connect('/sessiondestroy',
         ['controller'=>'Sessions','action'=>'destroy_session_data']);
      $routes->fallbacks('DashedRoute');
   });
   Plugin::routes();

src/Controller/目錄下創(chuàng)建一個(gè)SessionsController.php文件。復(fù)制以下代碼至其中。

src/Controller/SessionsController.php

<?php
   namespace AppController;
   use AppControllerAppController;

   class SessionsController extends AppController{
      public function retrieveSessionData(){
         //create session object
         $session = $this->request->session();
      
         //read data from session
         $name = $session->read('name');
         $this->set('name',$name);
      }
      public function writeSessionData(){
         //create session object
         $session = $this->request->session();
         
         //write data in session
         $session->write('name','Virat Gandhi');
      }
      public function checkSessionData(){
         //create session object
         $session = $this->request->session();
      
         //check session data
         $name = $session->check('name');
         $address = $session->check('address');
         $this->set('name',$name);
         $this->set('address',$address);
      }
      public function deleteSessionData(){
         //create session object
         $session = $this->request->session();
         
         //delete session data
         $session->delete('name');
      }
      public function destroySessionData(){
         //create session object
         $session = $this->request->session();
         
         //destroy session
         $session->destroy();
      }
   }
?>

src/Template目錄下創(chuàng)建一個(gè)Sessions目錄,并在該Sessions目錄下創(chuàng)建一個(gè)名為write_session_data.ctp視圖文件。復(fù)制以下代碼至其中。

src/Template/Sessions/write_session_data.ctp

The data has been written in session.

在該Sessions目錄下創(chuàng)建另一個(gè)名為retrieve_session_data.ctp視圖文件。復(fù)制以下代碼至其中
src/Template/Sessions/retrieve_session_data.ctp

Here is the data from session.
Name: <?=$name;?>

在該Sessions目錄下創(chuàng)建另一個(gè)名為check_session_data.ctp視圖文件,復(fù)制以下代碼至其中。

src/Template/Sessions/check_session_data.ctp
<?php if($name): ?>
name exists in the session.

<?php else: ?>
name doesn't exist in the database

<?php endif;?>
<?php if($address): ?>
address exists in the session.

<?php else: ?>
address doesn't exist in the database

<?php endif;?>

在該Sessions目錄下創(chuàng)建另一個(gè)名為delete_session_data.ctp視圖文件,復(fù)制以下代碼至其中。

src/Template/Sessions/delete_session_data.ctp

Data deleted from session.

在該Sessions目錄下創(chuàng)建另一個(gè)名為destroy_session_data.ctp視圖文件,復(fù)制以下代碼至其中。

src/Template/Sessions/destroy_session_data.ctp

Session Destroyed.

輸出

通過訪問以下網(wǎng)址執(zhí)行上面的例子。此URL將幫助你在會(huì)話上寫入數(shù)據(jù)。

http://locaohost:85/CakePHP/session-write


請(qǐng)?jiān)L問以下網(wǎng)址讀取會(huì)話數(shù)據(jù) - http://locaohost:85/CakePHP/session-read


請(qǐng)?jiān)L問以下網(wǎng)址查詢會(huì)話數(shù)據(jù) - http://locaohost:85/CakePHP/session-check


請(qǐng)?jiān)L問以下網(wǎng)址中刪除會(huì)話數(shù)據(jù) - http://locaohost:85/CakePHP/session-delete


請(qǐng)?jiān)L問以下網(wǎng)址銷毀會(huì)話數(shù)據(jù) - http://locaohost:85/CakePHP/session-destroy



以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)