John Davidson

php - how to configure storage in lumen 7.0

0 comments
Message:


In laravel we can use storage Facade in order to save and read files, but in lumen 7.0 there is no filesystem config available at start.


what I did so far:



  1. composer require league/flysystem

  2. in composer.json file, I added the following in autoload section:



"files": [
"app/helpers.php"
],


  1. then in app directory, I've created helpers.php and added the following into it:


if (! function_exists('public_path')) {
/**
* Get the path to the public folder.
*
* @param string $path
* @return string
*/
function public_path($path = '')
{
return app()->make('path.public').($path ? DIRECTORY_SEPARATOR.ltrim($path, DIRECTORY_SEPARATOR) : $path);
}
}

if (! function_exists('storage_path')) {
/**
* Get the path to the storage folder.
*
* @param string $path
* @return string
*/
function storage_path($path = '')
{
return app('path.storage').($path ? DIRECTORY_SEPARATOR.$path : $path);
}
}


  1. I created config directory and I copied the filesystems.php from laravel in it

  2. then in order to register the configuration I added the following to bootstrap/app.php:


$app->singleton('filesystem', function ($app) {
return $app->loadComponent('filesystems', 'Illuminate\Filesystem\FilesystemServiceProvider', 'filesystem');
});
$app->instance('path.config', app()->basePath() . DIRECTORY_SEPARATOR . 'config');
$app->instance('path.storage', app()->basePath() . DIRECTORY_SEPARATOR . 'storage');
$app->instance('path.public', app()->basePath() . DIRECTORY_SEPARATOR . 'public');

After doing all changes that I made to lumen, when I try to use Storage Facade for example:


Storage::file($dir);

it will throw an error that says:



Class 'League\Flysystem\Adapter\Local' not found



What is wrong with my configuration?



My Answer:
> You can configure storage in Lumen 7.0 by following these steps:

1. Install the league/flysystem package by running the following command:

composer require league/flysystem


2. Create a new service provider by running the following command:

php artisan make:provider StorageServiceProvider


3. Open the newly created StorageServiceProvider.php file in the app/Providers directory and add the following code to register the storage service:
php
use Illuminate\Support\ServiceProvider;
use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Local;

class StorageServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton('filesystem', function ($app) {
return new Filesystem(new Local(storage_path()));
});
}
}


4. Register the newly created service provider in the bootstrap/app.php file:
php
$app->register(App\Providers\StorageServiceProvider::class);


5. You can now use the storage service in your application by injecting it into your controllers or classes:
php
use League\Flysystem\Filesystem;

class ExampleController extends Controller
{
protected $filesystem;

public function __construct(Filesystem $filesystem)
{
$this->filesystem = $filesystem;
}

public function storeFile()
{
$this->filesystem->write('example.txt', 'Hello, World!');
}

public function readFile()
{
$contents = $this->filesystem->read('example.txt');
return $contents;
}
}


By following these steps, you can configure storage in Lumen 7.0 using the league/flysystem package.

Rate this post

3 of 5 based on 8397 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us