How to Change the Default Storage Path in Laravel

How to Change the Default Storage Path in Laravel

https://ift.tt/38WQNjv

It would be nice if Laravel provided a configuration entry to control which path is used for user uploaded content, or more generally, application data storage such as files, images, and binary data. In Laravel, this directory is known a the storage path

The helper function storage_path calls the container for a path.storage mapping. By default, Laravel populates this entry in Application.php – This is hard coded to directory relative to your applications root directory.

To change this, we can modify the storage path at runtime with a service provider. In this example, we’re modifying the storage path to /webroot/storage, which is the storage directory recommended for PHP apps hosted on Amezmo, but you may change this to any value.

 <?php namespace App\Providers; use Illuminate\Support\ServiceProvider; // Full path: app/Providers/AppServiceProvider.php class AppServiceProvider extends ServiceProvider { /** * Register any application services. * * @return void */ public function register() { $this->app->instance('path.storage', '/webroot/storage'); } /** * Bootstrap any application services. * * @return void */ public function boot() { // } }  

Now, every call to storage_path will return a path to our customized storage directory.

Why would we want to use the $instance property instead of the app helper function? The first reason is that it would be unnecessary to call app because it is identical to the instance property that we already have direct access to. Our service provider extends ServiceProvider, so we have access to the Application instance and we do not need to construct one by hand.

Another reason is that depending on your IDE, you can get nice autocompletion because of the PHP doc block defined on this property.

programming

via Laravel News Links https://ift.tt/2dvygAJ

February 3, 2020 at 10:25AM