Lumen, using the filesystem without facades

I’ve effectively dropped out of the Laravel community in the last 3 years. I had an excuse to use Lumen last week and found the information on using Lumen without facades rather lacking.

Here’s the trick, you need to register the manager into the service container.

Uncomment $app->register(App\Providers\AppServiceProvider::class); in bootstrap/app.php.

In app/Providers/AppServiceProvider.php add register the manager and add an alias.

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->singleton('filesystem', function ($app) {
            return $app->loadComponent('filesystems', 'Illuminate\Filesystem\FilesystemServiceProvider', 'filesystem');
        });

        $this->app->alias('filesystem', 'Illuminate\Filesystem\FilesystemManager');
    }
}

Now you’re free to use the FilesystemManager through dependency injection.

<?php

namespace App\Http\Controllers;

use Illuminate\Filesystem\FilesystemManager as Filesystem;

class ExampleController extends Controller
{
    public function index(Filesystem $filesystem)
    {
        $filesystem->disk('public')->put('test.html', '<h1>Teset</h1>');
    }
}

Using the public folder

Another nice feature of the Lumen integration of Flysystem is the addition of both local and public drives. One less obvious bit about the public drive is that you need to add a symlink between /public/storage and /storage/app/public your self.

cd ./public
ln -s ../storage/app/public storage