Laravel 4:Asset::add 的替代品是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14205404/
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-09 02:21:42  来源:igfitidea点击:

Laravel 4: What is the replacement for Asset::add?

laravellaravel-4

提问by sehummel

How do you manage assets in the new Laravel 4? It looks like Taylor Otwell has replaced Asset::addwith something new.

你如何在新的 Laravel 4 中管理资产?看起来 Taylor Otwell 已经换Asset::add了新的东西。

What I am trying to do is have Laravel add my CSS and JS files. There were several ways to do this in Laravel 3, but they seem to be gone now. One was Asset::addand the other was HTML. What are the replacements for these?

我想要做的是让 Laravel 添加我的 CSS 和 JS 文件。在 Laravel 3 中有几种方法可以做到这一点,但它们现在似乎已经消失了。一个是Asset::add,另一个是HTML。这些有什么替代品?

回答by Ben Bos

In Laravel 4 you can use the HTMLclass, it's default included in the Laravel Framework package:

在 Laravel 4 中,您可以使用HTML该类,它默认包含在 Laravel 框架包中:

Stylesheet:

样式表:

{{ HTML::style('css/style.css') }}

Javascript:

Javascript:

{{ HTML::script('js/default.js') }}

回答by Sandy Wilbourn

Laravel 4 doesn't have out of the box asset management. But some packages have been created to handle this kind of thing.

Laravel 4 没有开箱即用的资产管理。但是已经创建了一些包来处理这种事情。

  • codesleeve/asset-pipeline(my favorite so far)

  • jasonlewis/basset (had some errors with this)

  • teepluss/asset (closest to Laravel 3 Asset::add() but doesn't do concatenation or minification)

  • way/guard-laravel (requires ruby guard gem to run)

  • 代码套/资产管道(到目前为止我最喜欢的)

  • jasonlewis/basset(对此有一些错误)

  • teepluss/asset(最接近 Laravel 3 Asset::add() 但不进行串联或缩小)

  • way/guard-laravel(需要运行 ruby​​ guard gem)

回答by Dirk

I use the helper:

我使用助手:

<script src="{{ asset('js/jquery-1.9.1.js') }}"></script>
<script src="{{ asset('js/bootstrap.min.js') }}"></script>

See also

也可以看看

vendor/laravel/framework/src/Illuminate/Support/helpers.php

Many other helpers too, e.g. app_path(), various array tools, link_to_route(), link_to_action(), storage_path(), etc.

还有许多其他帮助程序,例如 app_path()、各种数组工具、link_to_route()、link_to_action()、storage_path() 等。

回答by Stelian

This is how I did when I needed to append some .css and .js files only to a specific page:

当我需要将一些 .css 和 .js 文件附加到特定页面时,我就是这样做的:

In my blade template:

在我的刀片模板中:

<head>
<title>.....</title>
.....
@yield('head')
</head>

And in my specific page blade file:

在我的特定页面刀片文件中:

@extends('template')
    @section('head')
    {{ HTML::style('css/filename.css') }}
    {{ HTML::script('js/filename.js') }}
    @stop
...

回答by Antonio Carlos Ribeiro

What about Basset (former Best Asset)?: http://jasonlewis.me/code/basset

Basset(前最佳资产)怎么样?:http: //jasonlewis.me/code/basset

I just installed it on L4, still no tests made, but sound promissing.

我刚刚将它安装在 L4 上,仍然没有进行测试,但听起来很有希望。

回答by Tee Plus

A port of Laravel 3's Asset class. Made to work with Laravel 4.

Laravel 3 的 Asset 类的一个端口。与 Laravel 4 一起使用。

https://github.com/teepluss/laravel4-asset.git

https://github.com/teepluss/laravel4-asset.git

回答by ebelendez

I made a much simpler version of Laravel 3 Asset class and works perfect in Laravel 4. For me it's all I need! Simply choose a name for the container and add the assets in the final order:

我制作了一个更简单的 Laravel 3 Asset 类版本,并在 Laravel 4 中完美运行。对我来说,这就是我所需要的!只需为容器选择一个名称并按最终顺序添加资产:

Asset::container('jq_1.10')->add('js/jquery-1.10.1.min.js')->add('css/css.css');

For output:

对于输出:

echo Asset::container('jq_1.10')->asset();

The class:

班上:

class Asset {
public static $containers = array();

public static function container($container = 'default')
{
    if ( ! isset(static::$containers[$container]))
    {
        static::$containers[$container] = new Asset_Container($container);
    }
    return static::$containers[$container];
}
}

class Asset_Container {
public $name;
public $assets = array();

public function __construct($name)
{
    $this->name = $name;
}
public function add($source)
{
    $type = (pathinfo($source, PATHINFO_EXTENSION) == 'css') ? 'style' : 'script';
    $obj = (object)array('type' => $type,'source' => $source);
    $this->assets[] = $obj;
    return $this;
}
public function asset()
{
    $str = '';
    foreach($this->assets as $aset){
        if($aset->type == 'style')$str .= HTML::style($aset->source);
        else $str .= HTML::script($aset->source);
    }
    return $str;
}
}

回答by bgallagh3r

I know it's already been answered, but Orchestra's asset package is identical to the Asset library Laravel 3 used. Might even have been the same package. It's what I use.

我知道已经有人回答了,但是 Orchestra 的资产包与 Laravel 3 使用的资产库相同。甚至可能是同一个包裹。这是我用的。

https://github.com/orchestral/asset

https://github.com/orchestral/asset

回答by Sophy

It will error if you use:

如果你使用它会出错:

{{ HTML::style('assets/css/style.css') }} 

Because Laravel change Laravel Base Aliases of HTMLto Html. If you got error the same me use code below to instead.

因为 Laravel 将 Laravel Base AliasesHTML改为Html. 如果你有同样的错误,我使用下面的代码来代替。

Style:

风格:

{{ Html::style('assets/css/style.css') }}

Script:

脚本:

{{ Html::script('assets/css/style.css') }}

回答by Ryan Di

In my opinion the best way of doing this, is with the URL::asset()method.

在我看来,最好的方法是使用URL::asset()方法。

For instance:

例如:

CSS

CSS

<link rel="stylesheet" href="{{URL::asset('css/style.css')}}" media="screen" />

or

或者

JS

JS

<script type="text/javascript" src="{{URL::asset('js/scripts.js')}}"></script>  

You can also use it with images and basically any asset located in your public folder.

您还可以将它用于图像和位于公共文件夹中的基本上任何资产。

ie: <img scr="{{URL::asset('img/image.jpg')}}}} id="someid">

<img scr="{{URL::asset('img/image.jpg')}}}} id="someid">

By using this method, i find it easier to organise and separate my HTML markup from Laravel methods.

通过使用这种方法,我发现从 Laravel 方法中组织和分离我的 HTML 标记更容易。

Say if i wanted to specify a css file that is to be loaded with the media="print" attribute, the "Laravel" way of doing this is the following:

假设我想指定一个使用 media="print" 属性加载的 css 文件,“Laravel”的方法如下:

{{HTML::style("print.css", array('media' => 'print'))}}

While the conventional way, using the {{URL::asset()}} method, is more comprehensive and accessible to front end developers(Which you may be working with in a team project).

而传统的方法,使用 {{URL::asset()}} 方法,更全面,前端开发人员可以访问(您可能在团队项目中使用)。

 <link rel="stylesheet" href="{{URL::asset('css/style.css')}}" media="print" />

You may say that the difference is minimal, but when dealing with plenty of markup attributes ("ids, data-attributes, classes custom-attributes"), the HTML::style(), HTML::image(), HTML::link() methods can turn out to be quite long, and in-comprehensive to html/css/js developers.

您可能会说差异很小,但是在处理大量标记属性(“ids、data-attributes、classes custom-attributes”)时,HTML::style()、HTML::image()、HTML:: link() 方法可能会很长,并且对 html/css/js 开发人员来说是不全面的。

It all varies and depends on the developer's style of coding, but in my opinion markup should remain as "raw" as possible, so that it is more accessible to front-end developers.

这一切都因开发人员的编码风格而异,但在我看来,标记应尽可能保持“原始”状态,以便前端开发人员更容易访问。