|
| 1 | +# Getting started |
| 2 | + |
| 3 | +- [Installation](#installation) |
| 4 | +- [Basic usage](#basic-usage) |
| 5 | +- [Defining multiple filesystems](#defining-multiple-filesystems) |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +flysystem-bundle requires PHP 7.1+ and Symfony 4.2+. |
| 10 | + |
| 11 | +You can install the bundle using Symfony Flex: |
| 12 | + |
| 13 | +``` |
| 14 | +composer require tgalopin/flysystem-bundle |
| 15 | +``` |
| 16 | + |
| 17 | +## Basic usage |
| 18 | + |
| 19 | +The default configuration file created by Symfony Flex provides enough configuration to |
| 20 | +use Flysystem in your application as soon as you install the bundle: |
| 21 | + |
| 22 | +```yaml |
| 23 | +# config/packages/flysystem.yaml |
| 24 | + |
| 25 | +flysystem: |
| 26 | + default_filesystem: 'app' |
| 27 | + filesystems: |
| 28 | + app: |
| 29 | + adapter: 'flysystem.adapter.local' |
| 30 | +``` |
| 31 | +
|
| 32 | +This configuration creates a single filesystem service, configured using the local adapter in the `storge` directory, |
| 33 | +and provides this service for autowiring using the `League\Flysystem\FilesystemInterface` interface. |
| 34 | +This autowiring will target the default filesystem defined in the bundle configuration. |
| 35 | + |
| 36 | +This means that if you are using autowiring, you can typehint `League\Flysystem\FilesystemInterface` in any |
| 37 | +of your services to get the default filesystem: |
| 38 | + |
| 39 | +```php |
| 40 | +use League\Flysystem\FilesystemInterface; |
| 41 | +
|
| 42 | +class MyService |
| 43 | +{ |
| 44 | + private $filesystem; |
| 45 | + |
| 46 | + public function __construct(FilesystemInterface $filesystem) |
| 47 | + { |
| 48 | + $this->filesystem = $filesystem; |
| 49 | + } |
| 50 | + |
| 51 | + // ... |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +The same goes for controllers: |
| 56 | + |
| 57 | +```php |
| 58 | +use League\Flysystem\FilesystemInterface; |
| 59 | +
|
| 60 | +class MyController |
| 61 | +{ |
| 62 | + public function index(FilesystemInterface $filesystem) |
| 63 | + { |
| 64 | + // ... |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +If you are not using autowiring, you can inject the `flysystem` service into your services |
| 70 | +manually to get the default filesystem. |
| 71 | + |
| 72 | +## Defining multiple filesystems |
| 73 | + |
| 74 | +Using a single filesystem is good way to get up and running quickly, but it is often useful to |
| 75 | +create multiple instances of Flysystem in order to manage different filesystems. |
| 76 | + |
| 77 | +This bundle provides this ability using `named aliases`: by leveraging the variable name in addition to |
| 78 | +the interface name, autowiring is able to inject the proper filesystem you want in your services. |
| 79 | + |
| 80 | +This means that if you are using autowiring, you can create multiple filesystems in the configuration of the |
| 81 | +bundle and typehint `League\Flysystem\FilesystemInterface` with a variable having the same name as your filesystem |
| 82 | +name to get this specific filesystem: |
| 83 | + |
| 84 | +```yaml |
| 85 | +# config/packages/flysystem.yaml |
| 86 | +
|
| 87 | +flysystem: |
| 88 | + default_filesystem: 'app' |
| 89 | + filesystems: |
| 90 | + app: |
| 91 | + adapter: 'flysystem.adapter.local' |
| 92 | +
|
| 93 | + # Defines an additional filesystem named "myFilesystem" |
| 94 | + myFilesystem: |
| 95 | + adapter: 'flysystem.adapter.local' |
| 96 | +``` |
| 97 | + |
| 98 | +```php |
| 99 | +use League\Flysystem\FilesystemInterface; |
| 100 | +
|
| 101 | +class MyController |
| 102 | +{ |
| 103 | + public function index(FilesystemInterface $fs, FilesystemInterface $myFilesystem) |
| 104 | + { |
| 105 | + // $fs is referencing the default filesystem ("app") because the variable name is not a filesystem name |
| 106 | + // $myFilesystem is referencing the "myFilesystem" filesystem |
| 107 | + } |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +If you are not using autowiring, you can inject the `flysystem.filesystem.<filesystem-name>` service into |
| 112 | +your services manually to get a specific filesystem (for instance here: `flysystem.filesystem.myFilesystem`). |
0 commit comments