有没有提供文件系统抽象层的Zend2库?

时间:2013-02-27 10:55:52

标签: zend-framework2

我正在寻找类似于:http://knplabs.com/blog/give-your-projects-a-gaufrette的内容 感谢。

1 个答案:

答案 0 :(得分:4)

只需使用Gaufrette,我也这样做(即使在ZF2项目中)!

php composer.phar require knplabs/gaufrette:0.1.*

然后,您可以在应用中的任何位置使用它,并最终通过在YourNamespace\Module#getServiceConfig中定义它来将其用作服务:

namespace YourNamespace;

use Zend\ModuleManager\Feature\ServiceProviderInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Gaufrette\Filesystem;
use Gaufrette\Adapter\Local as LocalFs;

class Module implements ServiceProviderInterface, ConfigProviderInterface
{
    public function getConfig()
    {
        return array(
            'your_namespace' => array(
                'filesystem' => array(
                    'base_path' => 'data/files',
                ),
            ),
        );
    }

    public function getServiceConfig()
    {
        return array(
            'factories' => array(
                'YourNamespace\Filesystem' => function (ServiceLocatorInterface $sl) {
                    $config   = $sl->get('Config');
                    $basePath = $config['your_namespace']['filesystem']['base_path'];

                    return new Filesystem(new LocalFs($basePath, true));
                },
            ),
        );
    }
}

然后,您可以在所有应用程序中使用服务YourNamespace\Filesystem

我还将它与Symfony\Filesystem结合使用来处理文件移动/复制/检查操作。并非所有内容都必须来自Zend Framework 2才能在ZF2应用程序中使用。

相关问题