Ruchita Sheth
Ruchita Sheth

Reputation: 860

asset() function return a path same as APP_URL

when I try to get a path of any media using asset('images/image1.jpg') it returns path as

'http://localhost/project1/images/image1.jpg'

instead of

'http://localhost/project1/public/images/image1.jpg'

here is a code that I am using to get image

<img src="{{ asset("images/image1.jpg") }}" />

Upvotes: 0

Views: 3033

Answers (2)

Mahesh Yadav
Mahesh Yadav

Reputation: 2684

In laravel 5, the assets folder refer to the public folder, located in the root directory path.

In public folder, you can see index.php, this file executed very first, when someone open the application.

For removing the "public" folder path in URL, we should create a virtual host and assign the root directory path to public path.

I have xampp installed in my system.For creating a new Virtual Host, you need to open the vhosts file.

In my case it's located under the path. E:\xampp\apache\conf\extra\httpd-vhosts.conf

Copy the below code in your httpd-vhosts file

<VirtualHost 127.0.0.1>
    ServerAdmin [email protected]
    DocumentRoot "E:/xampp/htdocs/[yourprojectname]/public"
    ServerName localhost.myproject
</VirtualHost>

You also need a entry in your windows hosts file. this file can be found under the below path.

C:\Windows\System32\drivers\etc\hosts

127.0.0.1 localhost.myproject

Restart your Xampp. Now your project can be accessed by "localhost.myproject" and you need not to add the public in your assets path.

Hope this all will help.

Upvotes: 1

Mohammad Fanni
Mohammad Fanni

Reputation: 4183

asset()

Generate a URL for an asset using the current scheme of the request (HTTP or HTTPS):

by default asset function doesn't return public as path

but you can add this path manually

<img src="{{ asset('public/images/image1.jpg) }}" />

Upvotes: 1

Related Questions