laravel 将资产文件链接到 Laravel4 中的视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23149763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:22:59  来源:igfitidea点击:

Linking asset files to views in Laravel4

laravellaravel-4

提问by Venkata Krishna

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.

嗨,我是新手,正在学习用于创建应用程序的 laravel 4。我正在尝试使用 laravel 刀片将 twitter bootstrap3 文件链接到视图。我安装了一个新的 Laravel 应用程序文件夹。

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.

要从 url 路径中删除 public,我删除了 public 文件夹中的所有文件,并将其保留在 public 文件夹之外。我根据 index.php 文件中的上述更改更改了路径。根据我的需要,我的应用程序中有两个部分,一个用于用户,另一个用于管理员。

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

因此,为此我更改了如下所示的 routes.php 文件。

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 和 AdminController 的管理员。这两个文件将如下所示。

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.indexto call admin index page when the url is like this http://localhost/laravel/admin.

现在我在 admin 部分的 views 文件夹下创建了 admin 文件夹。这就是为什么admin.index当 url 是这样的时候我一直调用管理索引页面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.phplike below.

现在我创建了资产文件夹,其中包含 css 文件的 css 文件夹、js 文件夹的 js 文件和图像的图像文件夹。我想将这些 css 和 js 文件链接到我的管理视图。所以我在layout.blade.php下面创建了一个喜欢。

<!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.

但资产没有链接。我可以知道我哪里出错了。

回答by Anil Sharma

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

如果您的资产文件夹位于 Laravel 的公共文件夹内,则使用以下命令:

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

假设您的文件夹结构是public/assets/css/bootstrap:然后

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

回答by Gareth Daine

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.

使用服务器或本地计算机上的虚拟主机从 URL 中删除 public。这很简单。只需谷歌一下如何为 Laravel 设置虚拟主机。

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

设置好虚拟主机后,只需将资产文件夹放在公共目录中即可。

Then in your Blade layout, use:

然后在您的 Blade 布局中,使用:

<link rel="stylesheet" href="{{ URL::asset('assets/css/bootstrap.css') }}">