Linking asset files to views in Laravel4

13,708

Solution 1

If your asset folder is inside public folder of laravel then user this:

Suppose your folder structure is public/assets/css/bootstrap: Then

 <script src="{{ URL::asset('assets/css/bootstrap.css') }}"></script>

Solution 2

Use a virtual host on your server or local machine to remove public from your URL. It's pretty straightforward. Simply Google on how to setup a virtual host for Laravel.

Once you have your virtual host setup, simply place your assets folder in your public directory.

Then in your Blade layout, use:

<link rel="stylesheet" href="{{ URL::asset('assets/css/bootstrap.css') }}">
Share:
13,708
Venkata Krishna
Author by

Venkata Krishna

A highly resourceful and competent PHP developer possessing expertise knowledge of building products and web applications LAMP stack technologies. Experienced in developing Web services, API's and working knowledge of social media API's like facebook, Twitter, LinkedIn etc and having the enthusiasm and ambition to complete the projects in high standard. Looking for s suitable developer position to effectively utilize the skills acquired so as to enhance the success of the organization and thereby generate mutual growth.

Updated on June 04, 2022

Comments

  • Venkata Krishna
    Venkata Krishna almost 2 years

    Hi I am newbie learning laravel 4 for creating an application. I am trying to link twitter bootstrap3 files to views using laravel blades. I installed a new laravel application folder.

    To remove public from url path i removed all the file in public folder and kept outside of public folder. I changed paths as per the above changes in index.php file.As per my needs i will have two sections in my application one is for users and another is for admin.

    So for this i changed my routes.php file like this below.

    Route::get('/', 'HomeController@index');
    
    Route::get('/admin', 'AdminController@index');
    

    I created two controllers one for users and another for admin named as HomeController and AdminController. Both files will look like below.

    HomeController for Users
    
    <?php
    
    class HomeController extends BaseController {
    
    
       public function index()
       {
        return View::make('index');
       }
    
     }
    
    AdminController for admin section
    
    <?php
    
    class AdminController extends BaseController {
    
    
        public function index()
        {
            return View::make('admin.index');
        }
    
    }
    

    Now i created admin folder under views folder for admin section. Thats why i kept admin.index to call admin index page when the url is like this http://localhost/laravel/admin.

    Now i created assets folder which has css folder for css files, js folder js files and images folder for images. I want to link these css and js files to my admin view. So i created a layout.blade.php like below.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Laravel</title>
    
    <link rel="stylesheet" href="{{ asset('assets/css/bootstrap.css') }}">
    <link rel="stylesheet" href="{{ asset('assets/css/style.css') }}">
    <script type="text/javascript" src="{{ asset('assets/js/jquery-1.10.1.min.js') }}"></script>
    <script type="text/javascript" src="{{ asset('assets/js/bootstrap.min.js') }}"></script>
    </head>
    
    <body>  
        @yield('content')
    </body>
    </html>
    

    After this i changed my view into like this below.

    @extends('layout')
    @section('content')
    <div class="container">
        <div class="row clearfix">
            <div class="col-md-12 column">
                <p>Here Comes Admin Section</p>
            </div>
        </div>
    </div>
    @stop
    

    But the assets are not linking. May i know where i am going wrong.

  • Venkata Krishna
    Venkata Krishna about 10 years
    Hey I removed public folder entirely and i kept assets folder in main application folder
  • Anil Sharma
    Anil Sharma about 10 years
    you are doing wrong there. Public folder should be their and inside public their would be index.php , along with them you can put your js css or assets.
  • Venkata Krishna
    Venkata Krishna about 10 years
    To remove public from url path i kept all the files inside public folder in main application folder and i removed empty public folder
  • Anil Sharma
    Anil Sharma about 10 years
    if you look at server.php inside laravel main folder you will find path to public/index.php. make public folder again and put index.php and your other assets files there. But still if you want to do it in this style then go to bootstrap/paths.php and make changes as accordingly
  • Venkata Krishna
    Venkata Krishna about 10 years
    hey I am installing the laravel application folder again but let me know how to remove public from url path
  • Anil Sharma
    Anil Sharma about 10 years
    I haven't done it before but yes you can simply do changes in paths from bootstrap/paths.php. this file contains your path information like your app folder, storage path , public path and base path. So experiment there!!!
  • Venkata Krishna
    Venkata Krishna about 10 years
    i tried in the way you said but that also not working
  • Anil Sharma
    Anil Sharma about 10 years
    you tried using public folder or changing path for public folder