Search code examples
laravelassets

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") }}" />

Solution

  • 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 myadav2005@gmail.com
        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.