Search code examples
phplaravellaravel-storage

Laravel: Storage::drive() and Storage::read()


I saw these Storage::drive() and Storage::read() in a Laravel 5.1 project, and I cannot find any info of these 2 on the internet.

can somehow explain or post the structure of these methods here?


Solution

  • The Storage Facade is for \Illuminate\Filesystem\FilesystemManager.

    When you are calling Storage::drive() Laravel are calling a instance of \Illuminate\Filesystem\FilesystemManager with the drive() method behind the scenes.

    However, the read() method does not exist on the FilesystemManager directly. It exists on another class \Illuminate\Contracts\Filesystem\Filesystem. When calling a method on the FilesystemManager that does not exist. PHP will use the magic method __call() inside of the FilesystemManager. Which in this case redirects the call to the Filesystem::drive() method.

    So a Storage::read() is more or less the same as a $filesystemManager->drive()->read().

    You can find some api documentation here.
    https://laravel.com/api/5.6/Illuminate/Filesystem/FilesystemManager.html
    https://laravel.com/api/5.6/Illuminate/Filesystem/Filesystem.html

    You can also find some Facade documentation here.
    https://laravel.com/docs/5.6/facades