<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] # 在session中存儲數據 session組件提供面向對象的包裝器來訪問session數據。 使用此組件而不是原生session的原因: * 您可以輕松地在同一域上的應用程序之間隔離session數據 * 攔截在您的應用程序中設置/獲取session數據的位置 * 根據應用程序需要更改session適配器 ## 啟動Session 某些應用程序是session密集型的,幾乎所有執行的操作都需要訪問session數據。還有其他人隨便訪問session數據。感謝服務容器,我們可以確保只在明確需要時訪問session: ```php <?php use Phalcon\Session\Adapter\Files as Session; // Start the session the first time when some component request the session service $di->setShared( 'session', function () { $session = new Session(); $session->start(); return $session; } ); ``` ## 工廠 使用`adapter`選項加載會話適配器類 ```php <?php use Phalcon\Session\Factory; $options = [ 'uniqueId' => 'my-private-app', 'host' => '127.0.0.1', 'port' => 11211, 'persistent' => true, 'lifetime' => 3600, 'prefix' => 'my_', 'adapter' => 'memcache', ]; $session = Factory::load($options); ``` ## 在Session中存儲/獲取數據 從控制器,視圖或任何其他繼承`Phalcon\Di\Injectable`的組件,您可以訪問session服務并存儲項目并按以下方式獲取它們: ```php <?php use Phalcon\Mvc\Controller; class UserController extends Controller { public function indexAction() { // Set a session variable $this->session->set('user-name', 'Michael'); } public function welcomeAction() { // Check if the variable is defined if ($this->session->has('user-name')) { // Retrieve its value $name = $this->session->get('user-name'); } } } ``` ## 刪除/銷毀Session 它也可以刪除特定變量或銷毀整個session: ```php <?php use Phalcon\Mvc\Controller; class UserController extends Controller { public function removeAction() { // Remove a session variable $this->session->remove('user-name'); } public function logoutAction() { // Destroy the whole session $this->session->destroy(); } } ``` ## 隔離應用程序之間的session數據 有時,用戶可以在同一session中在同一服務器上使用同一應用程序兩次。當然,如果我們在session中使用變量,我們希望每個應用程序都有單獨的session數據(即使相同的代碼和相同的變量名稱)。要解決此問題,您可以為在特定應用程序中創建的每個session變量添加前綴: ```php <?php use Phalcon\Session\Adapter\Files as Session; // Isolating the session data $di->set( 'session', function () { // All variables created will prefixed with 'my-app-1' $session = new Session( [ 'uniqueId' => 'my-app-1', ] ); $session->start(); return $session; } ); ``` 不需要添加唯一ID。 ## Session Bags `Phalcon\Session\Bag` 是一個幫助將會話數據分離到`命名空間`的組件。通過這種方式工作,您可以輕松地在應用程序中創建會話變量組。只需在包中設置變量,它就會自動存儲在會話中: ```php <?php use Phalcon\Session\Bag as SessionBag; $user = new SessionBag('user'); $user->setDI($di); $user->name = 'Kimbra Johnson'; $user->age = 22; ``` ## 組件中的持久數據 繼承 `Phalcon\Di\Injectable` 的控制器,組件和類可以注入`Phalcon\Session\Bag`。此類隔離每個類的變量。多虧了這一點,您可以以獨立的方式在每個類中的請求之間保留數據。 ```php <?php use Phalcon\Mvc\Controller; class UserController extends Controller { public function indexAction() { // Create a persistent variable 'name' $this->persistent->name = 'Laura'; } public function welcomeAction() { if (isset($this->persistent->name)) { echo 'Welcome, ', $this->persistent->name; } } } ``` 在一個組件中: ```php <?php use Phalcon\Mvc\User\Component; class Security extends Component { public function auth() { // Create a persistent variable 'name' $this->persistent->name = 'Laura'; } public function getAuthName() { return $this->persistent->name; } } ``` 添加到會話的數據(`$this->session`)在整個應用程序中都可用,而持久性(`$this->persistent`)只能在當前類的范圍內訪問。 ## 實現自己的適配器 必須實現`Phalcon\Session\AdapterInterface` 接口才能創建自己的會話適配器或擴展現有會話適配器。 [Phalcon Incubator](https://github.com/phalcon/incubator/tree/master/Library/Phalcon/Session/Adapter) 中有更多適用于此組件的適配器
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看