How to change public directory to public_html directory for Laravel Mix

Viewed 1173

I'm using Laravel 8 and I wanted to install Sweet Alert. So after downloading it and adding require('sweetalert'); to bootstrap.js, I ran the command npm run production.

Then I have included this in my master.blade.php:

<script src="{{ asset('/js/app.js') }}"></script>
@include('sweet::alert')

Now because I had changed my public directory from public to public_html, Laravel asset() function would call the public_html/js/app.js and this is wrong because Laravel Mix generated app.js and app.css in the public directory.

So the question is how can I change the default generation of Laravel Mix which is public to public_html ?

Here is my AppServiceProvider.php:

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

@Alfian has answered your question. But the solution you asked for, is not recommended when you are talking about renaming the public folder. You should not really rename your public folder while using Laravel.

But I can understand why you renamed it, to declare the default root folder. But there are better solutions instead of renaming it. And then there will be no such question too.

Best Solution: Upload the Laravel project as it is in the public_html folder. Go to your cPanel and enter Domains/Addon Domains/Subdomains (which type domain you are using) section and just change the Document Root as /public_html/public. You are done.

Update:

If it's a primary domain or addon domain there may not be an option directly to change the document root. Then we can just add a rule in the .htaccess file. Use this:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]

You can change configuration compiled assets directory on webpack config files, by default laravel set webpack at webpack.config.js

Here more documentation about compiling assets laravel

Laravel 8 Compiling Assets

In .env change ASSET_URL=https://yourdomain.com/public_html. But its a bad practice to have index along with php code(app, vendor, etc..)

You can create symbolic link from public to act as public html like follows

ln -s /full/work/path/laravel-project/public /full/work/path/public_html

But you might need to remove public_html first

mv public_html tmp

I assume your structure would be

full/work/path/
   - laravel-project
       - public
   - public_html
   ...

in the main root directory of laravel project one file server.php and another one index.php

first, delete the index.php file after that rename the server.php file to index.php

after that, you can use it directly as yourdomainname.example

From, dhayal info tech web solutions

Related