How to Use Gaufrette and Symfony 3.0

柔情痞子 提交于 2019-12-03 21:04:39
Konstantinos Tsatsarounos

I recommend you read this article: https://blog.fortrabbit.com/new-app-cloud-storage-s3

Is is a quick start guide that talks about why you could use decentralized storage and covers the following topics:

  • How to sign up for an AWS account
  • How to create you first S3 bucket storage container — name space for your files
  • How to set up proper permissions — safe access with additional credentials

Alternatively, I have had good experiences with the LeaguePHP Adapter:

League\Flysystem\AwsS3v3

It provides a simple api for using the amazon web services for files and stuff! It's compatible standalone or using Symfony or laravel. Check out the documentation. You can see the methods from the source folder.

Don't inject a service into an entity: it is bad practice.

Use Doctrine event subscribers instead: as described on Symfony docs

# app/config/services.yml
services:
    # ...
    AppBundle\EventListener\SearchIndexerSubscriber:
        tags:
            - { name: doctrine.event_subscriber }

Event subscriber:

// src/AppBundle/EventListener/SearchIndexerSubscriber.php
namespace AppBundle\EventListener;

use AppBundle\Entity\Product;
use Doctrine\Common\EventSubscriber;
// for Doctrine < 2.4: use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
use Doctrine\ORM\Events;

class SearchIndexerSubscriber implements EventSubscriber
{
    public function getSubscribedEvents()
    {
        return [
            Events::postPersist,
            Events::postUpdate,
        ];
    }

    public function postUpdate(LifecycleEventArgs $args)
    {
        $this->index($args);
    }

    public function postPersist(LifecycleEventArgs $args)
    {
        $this->index($args);
    }

    public function index(LifecycleEventArgs $args)
    {
        $entity = $args->getObject();

        // perhaps you only want to act on some "Product" entity
        if ($entity instanceof Product) {
            $entityManager = $args->getObjectManager();
            // ... do something with the Product
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!