Reputation: 310
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?
Upvotes: 1
Views: 10751
Reputation: 2478
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
Upvotes: 3
Reputation: 16
<link href="{{ asset('css/app.css') }}" rel="stylesheet">
This works just fine. When Laravel runs on the server the main index file and all assets are in the “public directory”. So the css asset files would start with domain name + css/file.css not domain/public/css/file.css.
Upvotes: 0
Reputation: 310
This is what worked for me:
To url
in config\app.php
I entered the full URL to the public
directory like this:
'url' => env('APP_URL', 'http://localhost/l5.5/public'),
I also entered the same URL to APP_URL
in the .env
file because that is what I'm using for configuration.
Apparently asset()
and similar functions append to the URL in the configuration file when constructing the file URL
UPDATE:
The problem with doing it this way is that I'm now having to edit Illuminate\Foundation\helpers.php
which I believe is a bad idea. I believe core files should not be touched.
Now instead of
function asset($path, $secure = null)
{
return app('url')->asset($path, $secure);
}
I am using
function asset($path)
{
return config('app.url'). '/' .$path;
}
May someone tell me the proper way of doing this?
Upvotes: 1
Reputation: 106
just do this
<link href="{{ asset('css/app.css') }}" rel="stylesheet">
Upvotes: 0
Reputation: 2485
In root folder rename server.php to index.php and copy .htaccess file from public folder to root folder. Also share your directory structure to show where your assets are places.
Upvotes: 0