> Symfony中文手册 > 配置Session文件的保存目录

配置Session文件的保存目录

默认情况下,symfony标准版使用php.ini的全局的session.save_handlersession.save_path值来决定session数据存储在哪里?这是因为下面的配置所致:

1
2
3
4
5
# app/config/config.yml
framework:
    session:
        # handler_id set to null will use default session handler from php.ini
        handler_id: ~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!-- app/config/config.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="Http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:framework="http://symfony.com/schema/dic/symfony"
    xsi:schemaLocation="http://symfony.com/schema/dic/services
        http://symfony.com/schema/dic/services/services-1.0.xsd
        http://symfony.com/schema/dic/symfony
        http://symfony.com/schema/dic/symfony/symfony-1.0.xsd"
>
    <framework:config>
        <!-- handler-id set to null will use default session handler from php.ini -->
        <framework:session handler-id="null" />
    </framework:config>
</container>
1
2
3
4
5
6
7
// app/config/config.php
$container->loadFromExtension('framework', array(
    'session' => array(
        // handler_id set to null will use default session handler from php.ini
        'handler_id' => null,
    ),
));

在这个配置中,改变您的Session元数据的存储位置完全取决于您的php.ini配置。

然而,如果你有下面的配置,symfony将保存session数据到缓存目录%kernel.cache_dir%/sessions中的文件夹里。这意味着当你清空这个缓存,所有的当前session也将被删除了:

1
2
3
# app/config/config.yml
framework:
    session: ~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!-- app/config/config.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:framework="http://symfony.com/schema/dic/symfony"
    xsi:schemaLocation="http://symfony.com/schema/dic/services
        http://symfony.com/schema/dic/services/services-1.0.xsd
        http://symfony.com/schema/dic/symfony
        http://symfony.com/schema/dic/symfony/symfony-1.0.xsd"
>
    <framework:config>
        <framework:session />
    </framework:config>
</container>
1
2
3
4
// app/config/config.php
$container->loadFromExtension('framework', array(
    'session' => array(),
));

使用另一个目录来保存session数据是一个方法,他能确保当你清除symfony缓存时你当前的session不会丢失。

使用不同的session保存操作在symfony的session管理中是很好的(也更复杂)方法。到“ Configuring Sessions and Save Handlers”看看session保存操作的详述。这里还有几篇文章是讲解如何存储session到关系型数据库或者Nosql数据库

改变 Symfony 存储session数据的目录,你只需要改变框架配置(framework configuration)。在这个例子中,你可以改变session目录为app/sessions

1
2
3
4
5
# app/config/config.yml
framework:
    session:
        handler_id: session.handler.native_file
        save_path: '%kernel.root_dir%/sessions'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!-- app/config/config.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:framework="http://symfony.com/schema/dic/symfony"
    xsi:schemaLocation="http://symfony.com/schema/dic/services
        http://symfony.com/schema/dic/services/services-1.0.xsd
        http://symfony.com/schema/dic/symfony
        http://symfony.com/schema/dic/symfony/symfony-1.0.xsd"
>
    <framework:config>
        <framework:session handler-id="session.handler.native_file"
            save-path="%kernel.root_dir%/sessions"
        />
    </framework:config>
</container>
1
2
3
4
5
6
7
// app/config/config.php
$container->loadFromExtension('framework', array(
    'session' => array(
        'handler_id' => 'session.handler.native_file',
        'save_path'  => '%kernel.root_dir%/sessions',
    ),
));