4

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';
        });
    }
nitinos
  • 80
  • 10
  • Laravel is written to assume the public directory is named `public`. Changing it is a bad idea. – James Clark Jan 12 '22 at 22:00
  • 4
    Can't you create a symlink from public_html to public? This will solve your problem without creating another problems – Elias Soares Jan 16 '22 at 20:43
  • @EliasSoares I don't get what you mean by symlink! Would you explain more plz? – nitinos Jan 17 '22 at 07:54
  • @nitinos Mix has a solution for this built-in. I've added an answer below. – D1__1 Jan 17 '22 at 10:24
  • do check how symbolic link used here: [config](https://gist.github.com/johanvanhelden/29096a9ff31eac9b039d36056919abbb) and other stuff here : [laravel mix](https://laravel-mix.com/docs/6.0/faq) It will help you understand the things plus symlink is actually a symbolic link and comes in very handy when you have multiple framework installations. By creating symbolic links you create alias for directories without changing actual name.check here [symlink](https://www.freecodecamp.org/news/symlink-tutorial-in-linux-how-to-create-and-remove-a-symbolic-link/) – wui Jan 18 '22 at 19:56
  • A better solution for you: https://stackoverflow.com/questions/30198669/how-to-change-public-folder-to-public-html-in-laravel-5 – Faizan Ali Jan 19 '22 at 14:11

4 Answers4

4

@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]
Tahasin
  • 338
  • 1
  • 11
2

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

justmealf
  • 35
  • 1
  • 4
0

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
   ...
A. Khaled
  • 1,468
  • 16
  • 28
-1

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