The code, provided here, is a configuration file from Laravel 8 for handling filesystems. In this configuration file, you can define various filesystem disks, specify their properties, and set the default filesystem disk to be used by the framework.

PHP
<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Default Filesystem Disk
    |--------------------------------------------------------------------------
    |
    | Here you may specify the default filesystem disk that should be used
    | by the framework. The "local" disk, as well as a variety of cloud
    | based disks are available to your application. Just store away!
    |
    */

    'default' => env('FILESYSTEM_DRIVER', 'local'),

    /*
    |--------------------------------------------------------------------------
    | Filesystem Disks
    |--------------------------------------------------------------------------
    |
    | Here you may configure as many filesystem "disks" as you wish, and you
    | may even configure multiple disks of the same driver. Defaults have
    | been setup for each driver as an example of the required options.
    |
    | Supported Drivers: "local", "ftp", "sftp", "s3"
    |
    */

    'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],

        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
        ],

        's3' => [
            'driver' => 's3',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET'),
            'url' => env('AWS_URL'),
            'endpoint' => env('AWS_ENDPOINT'),
            'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
        ],

    ],

    /*
    |--------------------------------------------------------------------------
    | Symbolic Links
    |--------------------------------------------------------------------------
    |
    | Here you may configure the symbolic links that will be created when the
    | `storage:link` Artisan command is executed. The array keys should be
    | the locations of the links and the values should be their targets.
    |
    */

    'links' => [
        public_path('storage') => storage_path('app/public'),
    ],

];

Step 1: Default Filesystem Disk

PHP
'default' => env('FILESYSTEM_DRIVER', 'local'),
  • This section specifies the default filesystem disk that should be used by the framework. The env function is used to retrieve an environment variable, and it defaults to ‘local’ if the ‘FILESYSTEM_DRIVER’ environment variable is not set.

Step 2: Filesystem Disks

PHP
'disks' => [
    // ...
]
  • This section allows you to configure multiple filesystem disks. Laravel supports different drivers like ‘local’, ‘ftp’, ‘sftp’, and ‘s3’ for storing and retrieving files.

‘local’ Disk

PHP
'local' => [
    'driver' => 'local',
    'root' => storage_path('app'),
],
  • This section configures a disk named ‘local’ with the ‘local’ driver. The ‘root’ parameter defines the root directory for this disk, which is typically ‘storage/app’.

‘public’ Disk

PHP
'public' => [
    'driver' => 'local',
    'root' => storage_path('app/public'),
    'url' => env('APP_URL').'/storage',
    'visibility' => 'public',
],
  • Here, a disk named ‘public’ is configured. It uses the ‘local’ driver and sets the root directory to ‘storage/app/public’. The ‘url’ parameter specifies the public URL for files stored in this disk. ‘visibility’ is set to ‘public’, indicating that files stored in this disk are publicly accessible.

‘s3’ Disk

PHP
's3' => [
    'driver' => 's3',
    'key' => env('AWS_ACCESS_KEY_ID'),
    'secret' => env('AWS_SECRET_ACCESS_KEY'),
    'region' => env('AWS_DEFAULT_REGION'),
    'bucket' => env('AWS_BUCKET'),
    'url' => env('AWS_URL'),
    'endpoint' => env('AWS_ENDPOINT'),
    'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
],
  • This section configures an ‘s3’ disk for storing files on Amazon S3. It uses the ‘s3’ driver and requires various settings like access key, secret key, region, bucket name, URL, and endpoint for the S3 service.

Step 3: Symbolic Links

PHP
'links' => [
    public_path('storage') => storage_path('app/public'),
],
  • This section defines symbolic links that will be created when the storage:link Artisan command is executed. It specifies the location of the link and its target. In this example, it creates a symbolic link from ‘public/storage’ to ‘storage/app/public’.

Usage Examples:

  1. Default Filesystem Disk: You can use the default filesystem disk to store and retrieve files in your application. For example, when you upload a user’s profile picture, it might be stored in the ‘local’ disk by default.
  2. Multiple Disks: You can use different disks for different purposes. For instance, you might use the ‘public’ disk for storing user-generated content that needs to be publicly accessible, and the ‘s3’ disk for storing backups or sensitive files on Amazon S3.
  3. Symbolic Links: When you run the php artisan storage:link command, symbolic links will be created. For example, you can create a symbolic link from ‘public/storage’ to ‘storage/app/public’, allowing you to serve files from ‘public/storage’ to the web.

Pros and Cons:

Pros:

  1. Abstraction: Laravel’s filesystem abstraction makes it easy to work with files and storage across different platforms and drivers without changing your code.
  2. Configurability: You can easily configure and switch between different storage solutions (local, FTP, S3, etc.) based on your application’s needs.
  3. Symbolic Links: Laravel provides a convenient way to create symbolic links, making it simple to serve files to the web.
  4. Environment Variables: The use of environment variables for configuration allows for flexibility and security.

Cons:

  1. Complexity: Managing multiple disks and their configurations can become complex in large applications.
  2. External Dependencies: When using cloud storage drivers like Amazon S3, your application’s file storage is dependent on external services, which may introduce additional complexity and cost.
  3. Learning Curve: For developers new to Laravel, understanding the filesystem configuration and how to use different disks can be challenging.

In summary, this Laravel 8 filesystem configuration allows you to define and manage various storage disks with different drivers and settings, making it versatile and adaptable to your application’s needs. It simplifies file storage and retrieval while providing flexibility and configurability. However, it’s important to carefully choose the right storage solution based on your project’s requirements and to consider the potential complexities of managing multiple disks and external dependencies.

Leave a comment

Your email address will not be published. Required fields are marked *

Translate ยป