Linking asset files to views in Laravel4

你离开我真会死。 提交于 2019-12-10 10:42:33

问题


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.


回答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>



回答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') }}">


来源:https://stackoverflow.com/questions/23149763/linking-asset-files-to-views-in-laravel4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!