Assets not referencing to public folder (Laravel)
I was having same problem. This is due to moving of .htaccess file from public to root of the project in order to serve localhost/project
rather than localhost/project/laravel
. And needed to use public as well in the asset:
<link href="{{ asset('public/css/app.css') }}" rel="stylesheet">
Or, modify the asset function from /Illuminate/Foundation/helpers.php
if (! function_exists('asset')) {
/**
* Generate an asset path for the application.
*
* @param string $path
* @param bool $secure
* @return string
*/
function asset($path, $secure = null)
{
return app('url')->asset("public/".$path, $secure);
}
}
The preceding method is not good way. Anyway this would have been easier if there was config for setting asset path.
Laravel 8 asset helpers not working in my new project
Use proper path in laravel asset()
helper function.asset()
helper function access from app/public
directory.
<link href="{{ asset('css/app.css') }}" rel="stylesheet" type="text/css">
Public folder not working with Mix and Asset Laravel Helpers Ubuntu 21.04
If /public
appears in your URI, your site is misconfigured. The public/
dir should be the DocumentRoot
, so that /
in your browser shows the public/index.php
file.
Here's an example of how to configure your document root in both Apache and nginx.
Lastly, a warning - serving your site out of the project root (as you are now) instead of public/
is a security risk and you shouldn't do that.
laravel asset() doesnt find assets in public
Based on your comments:
I already have the folder assets in my public/storage folder
@GiuseppeP. Is the public/storage folder a symbolic link?
yes I made the symbolic link 'links' => [ public_path('storage') =>
storage_path('app/public'), ],
Your file is in the storage path.
This is accessible by using:
storage_path()
The
storage_path
function returns the fully qualified path to your
application'sstorage
directory. You may also use thestorage_path
function to generate a fully qualified path to a given file within thestorage
directory:
storage_path('app/public/assets/js/jquery-3.4.1.min.js')
Addendum
Alternatively, you can use the asset()
helper by prefixing the path with storage/...
. I.e:
The Public Disk
The public disk included in your application's
filesystems
configuration file is intended for files that are going to be publicly
accessible. By default, thepublic
disk uses the local driver and
stores its files instorage/app/public
.Once a file has been stored and the symbolic link has been created,
you can create a URL to the files using theasset
helper:
asset('storage/assets/js/jquery-3.4.1.min.js')
Add Public to asset path in Laravel
Before Laravel 5.7.14
Take a look at Illuminate\Routing\UrlGenerator
class and its asset($path, $secure = null)
method. This class is being put to container by url
key. What you can do is:
- Add your own class, extending
UrlGenerator
; - Add
asset($path, $secure = null)
method to your own class, making it return whatever you need; - Create a service provider and register it in
config/app.php
; - In your service provider's
register()
method, bind your new class
to the container byurl
key.
This way, you don't touch core files at all and your Laravel application is still update friendly.
Update for Laravel 5.7.14 and later
As other answers state, there is the ASSET_URL
.env option, which makes it much easier to change the public path. Laravel introduced it in Laravel 5.7.14, which was released about 3 years after my original answer.
In Laravel can I use a path to a public file directly without using asset or url functions?
if the style files are placed in the public directory, you can write the path to them without asset helper and everything will work. However, if the resources you need are outside the public directory, this helper is indispensable. I think the file storage documentation will suit you
Related Topics
Recursive Function to Get All the Child Categories
Create Table with PHP and Populate from MySQL
Detecting Whether a User Is Behind a Proxy
What Security Problems Could Come from Exposing PHPinfo() to End Users
Replace Deprecated Preg_Replace /E with Preg_Replace_Callback
How to Take an Array, Divide It by Two and Create Two Lists
$Path Environment Variable for Apache2 on MAC
PHP Curl How to Add the User Agent Value or Overcome the Servers Blocking Curl Requests
PHP Convert Decimal into Fraction and Back
PHP - Most Lightweight Psr-0 Compliant Autoloader
Prevent Direct Access to a PHP Page
How to Include File Outside Document Root
How to Setup a Cron Job in Magento Module
Remotely Connecting to a MySQL Database