I am having issues with assets in the /public directory in Laravel 5.4 and Laravel 5.5.
On a fresh install of Laravel, nothing in the public directory will work without /public in the URL.
When I run php artisan make:auth
, the views that are created are supposed to be perfect, right? Well, what I get is an unstyled page which only looks pretty when I use the DOM inspector to add /public so that I end up with:
<link href="http://localhost/l5.4/public/css/app.css" rel="stylesheet">
Where can I change the setting so that I don't have to add /public in the views each time I want to link to an asset?
You don't have to do that manually there is a function shipped with laravel: {{asset('css/app.css')}}
this access directly the public folder.
Note:
If that doesn't work then you're high probably need to set up a virtualHost that points on you /public
folder.
You can do that in your vHosts file on Apache.
Note2:
If you're on a shared hosting per example, or you can't modify your vHost file for a reason or an other, here is the solution you've got.
First, you have to put your public folder above the project folder and rename it per example public_folder, inside it, you'll have to put the content of the public folder.
/public_folder
/your_project_folder
app/
databases/
...
Second, inside the public folder you will find a file called index.php
you have to modify it as follows:
require __DIR__.'/../your_project_folder/bootstrap/autoload.php';
$app = require_once __DIR__.'/../your_project_folder/bootstrap/app.php';
That means you're referencing the project folder from the public folder we created above.
Note3:
To tell laravel that the asset function going to be referenced from the new location of our public folder, you can add this to your index.php, after $app=require_once...
:
$app->bind('path.public', function() {
return __DIR__;
});
Hope it makes sense