Home » vue » Change public directory laravel

Change public directory laravel

Posted by: admin November 26, 2021 Leave a comment

Questions:

I want to change the public folder to public_html, the main problem is that some commands and functions not work because it still point to public.

I am trying to use the laravel+vue to develop a spa. i normally rename the public folder to public_html and that’s all, but know it seems that i have to use the public_path helper and the console and these use the public folder too.

im using npm run dev as in the laravel docs, but it generate everything in the public folder, not in the public_html that i have renamed

so how can i tell the console and the public_path helper to do they magic in the public_html folder?

I am using Laravel 5.6, it is a fresh project, with just a controller and a view with a mix helper calling the js that i want link this <script src="{{ mix('js/app.js') }}"></script>.

Answers:

May this can help you. You can override Laravel default helper methods. This code helped me at a time when I got same problem like you.

Simple I created one helper file and override public_html function something like this:

/**
 * Get the path to the public folder.
 *
 * @param  string  $path
 * @return string
 */
function public_path($path = '')
{
    return base_path().'/public_html';
}

Let’s say file name is AppHelper.php and located into app/Http folder.
To override helper from base you need to include your helper file(AppHelper.php) before Laravel does it’s own autoload file.

So for the web you have to include your file into index.php like this:

require __DIR__.'/../app/Http/AppHelper.php';

require __DIR__.'/../vendor/autoload.php';

Remember your helper must be included before autoload file. and this code snippet for Command line or Artisan CLI:

require __DIR__.'/app/Http/AppHelper.php';

require __DIR__.'/vendor/autoload.php';

You have to write your artisan cli changes into artisan file which is on root.

In Laravel if you check helper function then each and every function wrapped with if condition like this function is not exist then declare it using function_exists()

In our case we already declare public_path function and included before Laravel autoload file so every request first execute our function.

Hope this can help you. Good luck.

###

Add this in register() method of your AppServiceProvider

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

For the css+js resources, as @Bagus Tesa said, you have a file named webpack.mix.js in the root of your application folder. It’s not you who created it, it comes with the laravel project. In that file you’ll find something like:

mix.js('resources/assets/js/app.js', 'public/js')
   .sass('resources/assets/sass/app.scss', 'public/css');

Change the destination path from public to public_html