Change the storage path in Laravel 5

后端 未结 6 963
星月不相逢
星月不相逢 2021-01-04 07:18

I want to change the storage path Laravel 5.1 uses to something like /home/test/storage. This has the advantage that these files are not stored in the repositor

相关标签:
6条回答
  • 2021-01-04 07:38

    Laravel 5.3 is in bootstrap/app.php

    /*
    |--------------------------------------------------------------------------
    | Set Storage Path
    |--------------------------------------------------------------------------
    |
    | This script allows us to override the default storage location used by
    | the  application.  You may set the APP_STORAGE environment variable
    | in your .env file,  if not set the default location will be used
    |
    */
    
    $app->useStoragePath( env( 'APP_STORAGE', base_path() . '/storage' ) );
    
    0 讨论(0)
  • 2021-01-04 07:38

    This works on Laravel 5.2

    File: app/Providers/AppServiceProvider.php

    public function register() {
      ...
      $this->app->useStoragePath(config('what_ever_you_want'));
      ...
    }
    
    0 讨论(0)
  • 2021-01-04 07:38

    Calling useStoragePath on your AppServiceProvider wouldn't do the job properly because the AppServiceProvider is called after the config files are loaded. so any use of storage_path in config files would still refer to the old storage path.

    In order to properly solve this problem I suggest you extend Application class and then on the constructor of your own class write the followings.

    
        /**
         * MyApplication constructor.
         */
        public function __construct($basePath = null)
        {
            parent::__construct($basePath);
            // set the storage path
            $this->afterLoadingEnvironment(function () {
                $this->useStoragePath(/*path to your storage directory*/);
            });
        }
    
    0 讨论(0)
  • 2021-01-04 07:43

    Set it up in .env

    app.php

    'app_storage' => env('APP_STORAGE', storage_path()),
    

    app/Providers/AppServiceProvider.php

    public function register()
    {
        $this->app->useStoragePath(config('app.app_storage'));
    }
    

    .env

    APP_STORAGE=custom_location
    
    0 讨论(0)
  • 2021-01-04 07:44

    if your website is hosted;

    move everything from public folder to root folder

    $app->bind('path.public', function() {
        return __DIR__;
    });
    
    0 讨论(0)
  • 2021-01-04 07:49

    Here is a simple solution of changing the storage path in Laravel 5 like we do in Laravel 4

    on bootstrap/app.php

    # new storage path
    # base_path() -> returns root path
    $path_storage = base_path() . "../../storage";
    
    # override already $app->storagePath using the function
    $app->useStoragePath($path_storage);
    

    this will make the storage path to be same with the session, views, cache, logs

    0 讨论(0)
提交回复
热议问题