Laravel 5 change public_path()

PhpLaravelLaravel 5

Php Problem Overview


I am trying to move the public folder to other place. However, I can't find the place to modify public_path() variable. Now, the 'public_path()' return the wrong path of folder.

Where can I set the variable for public_path()?

Php Solutions


Solution 1 - Php

You can override the public path using ioc container :

What worked for me flawlessly was adding to public/index.php the following three lines:

 $app->bind('path.public', function() {
    return __DIR__;
});

For more detailed explanation click here

Solution 2 - Php

While accepted answer works for requests coming from HTTP, it will not work for artisan.

If you need artisan to know your custom public path, you need to extend Laravel main Application class. I know it sounds scary, but it's actually very simple.

All you need to do is following:
Step 1: In the file: bootstrap/app.php change the very first declaration of $app variable

$app = new Illuminate\Foundation\Application(
    realpath(__DIR__.'/../')
);

to reflect your own custom Application class:

$app = new App\Application(
    realpath(__DIR__.'/../')
);

Step 2: Define your custom Application class somewhere. For example in app/Application.php

<?php namespace App;

class Application extends \Illuminate\Foundation\Application
{
}

Congrats! You have extended Laravel core Application class.

Step 3: Overwrite the publicPath method. Copy and paste Laravel original method to your new class and change it to your needs. In my particular case I did like this:

public function publicPath()
{
	return $this->basePath.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'public_html';
}

That's it! You can overwrite any method in Application class the same way.

Solution 3 - Php

I suggest you add it in app/Providers/AppServiceProvider.php:

public function register()
{
    $this->app->bind('path.public', function() {
        return realpath(base_path().'/../public_html');
    });
}

This affects artisan as well.

Solution 4 - Php

In laravel 5.6 this work for me ... adding this code to bootstrap/app.php :

$app->bind('path.public', function() {
    return realpath(__DIR__.'/../');
});

where DIR.'/../' is path to your public folder

Solution 5 - Php

In my case in Laravel 6.0 work this.
This file: bootstrap/app.php

......
$app = new Illuminate\Foundation\Application(
    $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__) );

$app->bind('path.public', function() {
    return realpath(__DIR__.'/../httpdocs'); });
.....

I changed the public_path() folder, this example is with httpdocs folder, you can put watherver you want.

Solution 6 - Php

$app->bind('path.public', function() {
  return base_path().'/mynewpublic';
});

Solution 7 - Php

Have you tried updating the filesystems.php

'disks' => [

	'public' => [
       'driver' => 'local',
			 'root' => env('PATH_PUBLIC'),
       'visibility' => 'public',
       'url' => env('SITE_ROOT'),
    ],

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1995781View Question on Stackoverflow
Solution 1 - PhpJigs ViraniView Answer on Stackoverflow
Solution 2 - PhpruuterView Answer on Stackoverflow
Solution 3 - PhpBroshiView Answer on Stackoverflow
Solution 4 - PhpAnasSafiView Answer on Stackoverflow
Solution 5 - PhpUnai SusperregiView Answer on Stackoverflow
Solution 6 - PhpmacieksView Answer on Stackoverflow
Solution 7 - PhpHarry BoshView Answer on Stackoverflow