The complete Lavarel framework development tutorial

Powered by Blogger.

Thursday 2 February 2017

Link external files to laravel views


While designing a web page, we often need to link external files like stylesheets, js files or images to a web page. If we are not using any framework, then we just put all the files to any folder and gives the path of that file to include into the web page. But in case of laravel the views are stored in a folder, which is not accessible for users. So, we can't just paste the files in resources folder, where we store the views.

In this tutorial, we will learn to link external files to laravel views. I assume that, you all know how to create a view in laravel. You can have a detailed tutorial on Laravel views by clicking on below link-

http://www.w3laravel.com/2016/07/laravel-views-tutorial-creating-view.html

Link external files to laravel views

To link external files to laravel view, first you need to copy all of your files to the public folder. This is not mandatory, but you can have three different folders like css, js and images for different kind of files. I assume that you have three new folders css, js and images and copied all the files to the corresponding folders. Now we need to include those files to views. We can use asset function to link all kind of files. Let's see how these can be done.

Link a css file

To link a css file, use assest to your head section of html-
<link href="{{ asset('css/css-file-name.css') }}" rel="stylesheet">
Here, in the asset function we need to write the path of the file excluding public folder.

Link a js file

To link a js file, use assest to the view-
<script type="text/javascript" src="{{ asset('js/jquery.js') }}"></script>
Here, in the asset function we need to write the path of the file excluding public folder.

Inserting an image to a view

To insert an image to a view, we can use asset function to attribute of img tag.
<img src="{{asset('images/image-name.png')}}" />
Here, in the asset function we need to write the path of the file excluding public folder.

Link files through CDN

To link files through CDN, we don't need anything to do extra. Just link files as you are linking in normal simple files.
<link rel="stylesheet" href="link-to-file">

Conclusion

So, by using asset function we can include all type of files to a laravel file. Share the tutorial Link external files to laravel views like stylesheets or images to help others too.

0 comments:

Post a Comment