69

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()?

7 Answers 7

82

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

3
  • 7
    Tried with your solution, it works on display webpages. However, it doesn't work for artisan command. The artisan still refer public_path to the default public path. Aug 1, 2015 at 15:55
  • 4
    @user1995781 you can apply the same code in the file "artisan" which is the "index.php" of the command.
    – Spir
    May 7, 2018 at 13:43
  • 2
    Thanks for providing the reference link too. Nov 28, 2019 at 10:33
49

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.

8
  • Regarding Step 3 ... where in /bootstrap/app.php should I be putting that override?
    – dcolumbus
    Nov 13, 2015 at 20:39
  • @dcolumbus This should not go to bootstrap/app.php file. It should be in you custom Application class, which is extending Laravel core (step 2) and therefor overriding Laravel core publicPath method.
    – ruuter
    Nov 16, 2015 at 8:32
  • I originally had the declaration in my index.php file, which worked for regular http requests. I came to an error when running PHPunit, it said "File (...) not defined in asset manifest". The reason was that it couldn't find the asset manifest because it wasn't looking in the right public folder. The solution you demonstrate here solved that. So it works for HTTP, artisan ánd PHPunit. It should really be the accepted answer! Sep 22, 2016 at 7:53
  • Scary yea, but it works. But i don't understand why this part '..'.DIRECTORY_SEPARATOR. I have this return $this->basePath.DIRECTORY_SEPARATOR.'public_html' for shared host. May 16, 2017 at 17:00
  • 1
    @FONGOHMARTIN .. is a name for parent directory in Linux, while . is a current directory. Which means, I have public_html dir one level up from the basePath, not inside it.
    – ruuter
    May 16, 2017 at 18:51
28

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.

2
  • why this affect the artisan? Jan 28, 2020 at 13:27
  • This doesn't work in some config cases! Be careful! But the index.php one worked. Jun 28, 2021 at 19:25
10

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

2
  • A lot of good solutions provided by this thread, I choose this one. Thanks. Feb 15, 2019 at 22:19
  • This one works just fine, help me to move a file to the public folder when I split the public folder from the laravel app folder. Thank you!
    – ynsmtkl
    Jan 11, 2022 at 13:05
4

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.

2
  • Only this one worked correctly for me. Compatible with artisan and HTTP requests. Good. Jul 4, 2021 at 1:05
  • @X4748-IR Same here. Laravel 8.40
    – Ikechukwu
    Jul 10, 2021 at 23:31
3
$app->bind('path.public', function() {
  return base_path().'/mynewpublic';
});
0

Have you tried updating the filesystems.php

'disks' => [

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.