使用 Session 存储数据 Storing data in Session

Phalcon\Session 提供对session数据面向对象的封装。使用这个组件替换原始session数据的原因有:

The Phalcon\Session provides object-oriented wrappers to access session data.

Reasons to use this component instead of raw-sessions:

  • 可以从同一个域名的不同应用中分离session数据
  • 在应用程序中设置/获取session数据时进行截获
  • 根据应用需求改变session的适配器
  • You can easily isolate session data across applications on the same domain
  • Intercept where session data is set/get in your application
  • Change the session adapter according to the application needs

启动会话Starting the Session

一些应用程序是session密集使用型的,几乎任何执行操作都需要访问session数据。也有session访问不密集型的。由于有服务容器,我们可以确保只要必须要访问session时才能正常访问到数据:

Some applications are session-intensive, almost any action that performs requires access to session data. There are others who access session data casually. Thanks to the service container, we can ensure that the session is accessed only when it’s clearly needed:

  1. <?php
  2. use Phalcon\Session\Adapter\Files as Session;
  3. //Start the session the first time when some component request the session service
  4. $di->setShared('session', function() {
  5. $session = new Session();
  6. $session->start();
  7. return $session;
  8. });

Session 的存储与读取 Storing/Retrieving data in Session

从一个控制器,一个视图或任何其他组件,只要是由 :doc:`Phalcon\DI\Injectable <../api/Phalcon_DI_Injectable>`扩展来的,使用以下方式你可以访问会话服务和存储和检索数据:

From a controller, a view or any other component that extends Phalcon\DI\Injectable you can access the session service and store items and retrieve them in the following way:

  1. <?php
  2. use Phalcon\Mvc\Controller;
  3. class UserController extends Controller
  4. {
  5. public function indexAction()
  6. {
  7. //Set a session variable
  8. $this->session->set("user-name", "Michael");
  9. }
  10. public function welcomeAction()
  11. {
  12. //Check if the variable is defined
  13. if ($this->session->has("user-name")) {
  14. //Retrieve its value
  15. $name = $this->session->get("user-name");
  16. }
  17. }
  18. }

Sessions 的删除和销毁Removing/Destroying Sessions

可以删除指定的数据或者是销毁整个session数据:

It’s also possible remove specific variables or destroy the whole session:

  1. <?php
  2. use Phalcon\Mvc\Controller;
  3. class UserController extends Controller
  4. {
  5. public function removeAction()
  6. {
  7. //Remove a session variable
  8. $this->session->remove("user-name");
  9. }
  10. public function logoutAction()
  11. {
  12. //Destroy the whole session
  13. $this->session->destroy();
  14. }
  15. }

隔离不同应用的会话数据Isolating Session Data between Applications

有时用户可以使用相同的应用程序两次,在同一台服务器上,在同一会话。当然,如果我们在会话中使用的变量,我们希望在每一个应用程序有单独的会话数据(即使相同的代码和变量名相同)。为了解决这个问题,可以在应用程序中为每个会话变量添加一个前缀:

Sometimes a user can use the same application twice, on the same server, in the same session. Surely, if we use variables in session, we want that every application have separate session data (even though the same code and same variable names). To solve this, you can add a prefix for every session variable created in a certain application:

  1. <?php
  2. use Phalcon\Session\Adapter\Files as Session;
  3. //Isolating the session data
  4. $di->set('session', function(){
  5. //All variables created will prefixed with "my-app-1"
  6. $session = new Session(
  7. array(
  8. 'uniqueId' => 'my-app-1'
  9. )
  10. );
  11. $session->start();
  12. return $session;
  13. });

会话袋Session Bags

Phalcon\Session\Bag 是一个可以分离会话数据到namespaces的组件。使用这个组件可以轻易的创建会话数据分组。只需要将变量设置为bag中的值就可以自动保存在会话中:

Phalcon\Session\Bag is a component that helps separating session data into “namespaces”. Working by this way you can easily create groups of session variables into the application. By only setting the variables in the “bag”, it’s automatically stored in session:

  1. <?php
  2. use Phalcon\Session\Bag as SessionBag;
  3. $user = new SessionBag('user');
  4. $user->setDI($di);
  5. $user->name = "Kimbra Johnson";
  6. $user->age = 22;

组件的持久数据Persistent Data in Components

继承自 Phalcon\DI\Injectable 的控制器、组件或者是类可以注入到 Phalcon\Session\Bag 中。这个类可以分离每个类中的变量。使用这个方法可以在请求之间持久化保存数据。

Controller, components and classes that extends Phalcon\DI\Injectable may inject a Phalcon\Session\Bag. This class isolates variables for every class. Thanks to this you can persist data between requests in every class in an independent way.

  1. <?php
  2. use Phalcon\Mvc\Controller;
  3. class UserController extends Controller
  4. {
  5. public function indexAction()
  6. {
  7. // Create a persistent variable "name"
  8. $this->persistent->name = "Laura";
  9. }
  10. public function welcomeAction()
  11. {
  12. if (isset($this->persistent->name))
  13. {
  14. echo "Welcome, ", $this->persistent->name;
  15. }
  16. }
  17. }

在一个组件中:

In a component:

  1. <?php
  2. use Phalcon\Mvc\Controller;
  3. class Security extends Component
  4. {
  5. public function auth()
  6. {
  7. // Create a persistent variable "name"
  8. $this->persistent->name = "Laura";
  9. }
  10. public function getAuthName()
  11. {
  12. return $this->persistent->name;
  13. }
  14. }

添加到session中($this->session)的数据整个应用中都可以访问,但是持久化的数据($this->persistent)只有在当前类的作用域中才可以访问。

The data added to the session ($this->session) are available throughout the application, while persistent ($this->persistent) can only be accessed in the scope of the current class.

自定义适配器Implementing your own adapters

:doc:`Phalcon\Session\AdapterInterface <../api/Phalcon_Session_AdapterInterface>`接口必须被继承实现如果想要定义自己的会话适配器。 `Phalcon Incubator <https://github.com/phalcon/incubator/tree/master/Library/Phalcon/Session/Adapter>`_在这个中介绍了更多的适配器。

The Phalcon\Session\AdapterInterface interface must be implemented in order to create your own session adapters or extend the existing ones.

There are more adapters available for this components in the Phalcon Incubator