在 Laravel 5 中,{{url}} 和 {{asset}} 有什么区别?

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

In Laravel 5, What's the difference between {{url}} and {{asset}}?

phplaravellaravel-5.2

提问by u6772460

I find sometimes both is OK? So what's the really difference?

我发现有时两者都可以?那么真正的区别是什么?

For example,

例如,

<link rel="stylesheet" href="{{asset('resources/views/admin/style/css-ui.admin.css')}}">

and

<link rel="stylesheet" href="{{url('resources/views/admin/style/font/css/font-awesome.min.css')}}">

these two form is both OK.

这两种形式都可以。

So, what's the differences?

那么,有什么区别呢?

回答by Qevo

Deciding which URL helperto use

决定哪些URL帮手来使用

Consider the type of URL that is needed / how the URL is being used. One of the advantages of having separate helper methods for each type of URL is they can have different handling logic. For example, assets (e.g. CSS, images, etc.) could involve a check that the file exists in the file system but do not require the type of analysis that a route would because the route may have parameters.

考虑所需的 URL 类型/URL 的使用方式。为每种类型的 URL 使用单独的辅助方法的优点之一是它们可以具有不同的处理逻辑。例如,资产(例如 CSS、图像等)可能涉及检查文件是否存在于文件系统中,但不需要路由的分析类型,因为路由可能具有参数。

url()Generates an absolute URL to the given path (code)

url()生成给定路径的绝对 URL (代码)

  • Use for static URLs (which should be rare).
  • Accepts array of parameters that are encoded and added to the end of the domain.
  • Preserves any URL query string.

    {{ url('search') }}
    // http://www.example.com/search
    
    {{ url('search', ['qevo', 'laravel']) }}
    // http://www.example.com/search/qevo/laravel
    
  • 用于静态 URL(应该很少见)。
  • 接受编码并添加到域末尾的参数数组。
  • 保留任何URL 查询字符串

    {{ url('search') }}
    // http://www.example.com/search
    
    {{ url('search', ['qevo', 'laravel']) }}
    // http://www.example.com/search/qevo/laravel
    

asset()Generates a URL to an application asset (code)

asset()生成应用程序资产的 URL (代码)

  • Use for files that are directly served such as CSS, images, javascript.
  • Only accepts a direct path.

    {{ asset('css/app.css') }}
    // http://www.example.com/css/app.css
    
  • 用于直接提供的文件,如 CSS、图像、javascript。
  • 只接受直接路径。

    {{ asset('css/app.css') }}
    // http://www.example.com/css/app.css
    

route()Gets the URL to a named route (code)

route()获取指定路由的 URL (代码)

  • Use for every route (every route should be named to help future-proof path changes).
  • Requires named routes.
  • Accepts associative array for route parameters.
  • Allows override for relative route vs. absolute route (default).

    {{ route('user.profile', ['name'=>'qevo']) }}
    // http://www.example.com/user/qevo/profile
    
    {{ route('user.profile', ['name'=>'qevo'], false) }}
    // /user/qevo/profile
    
  • 用于每条路线(每条路线都应该命名以帮助未来的路径更改)。
  • 需要命名路由
  • 接受路由参数的关联数组。
  • 允许覆盖相对路由与绝对路由(默认)。

    {{ route('user.profile', ['name'=>'qevo']) }}
    // http://www.example.com/user/qevo/profile
    
    {{ route('user.profile', ['name'=>'qevo'], false) }}
    // /user/qevo/profile
    

回答by Connor Gurney

{{url}}allows you to create a link to a URL on your site -- another benefit is the fact that you can set the second parameter to an array with query string parameters within.

{{url}}允许您在站点上创建指向 URL 的链接——另一个好处是您可以将第二个参数设置为包含查询字符串参数的数组。

{{asset}simply allows you to link to an asset within your public directory -- for example css/main.css.

{{asset}只是允许您链接到公共目录中的资产——例如css/main.css

回答by Fefar Ravi

asset()asset function generates a url for an asset using the current scheme of request. Ex : asset('images/img.png')

asset()资产功能使用当前的请求方案为资产生成一个 url。 例如:资产('图像/img.png')

url()The url function generates a fully qualified URL to the given path.

url()url 函数为给定的路径生成一个完全限定的 URL。

Ex : url('admin/users')

例如: url('管理员/用户')

回答by tisuchi

URL::routegets the URL to a named route. So in your case if you name your route like this:

URL::route获取命名路由的 URL。所以在你的情况下,如果你像这样命名你的路线:

Route::get('/account/register', [
    'name' => 'register', 
    'uses' => 'RegisterController@create'
]);

then you will be able to use

然后你就可以使用

<a href="{{ URL::route('register') }}" >Register 1</a>in Blade templates.

<a href="{{ URL::route('register') }}" >Register 1</a>在 Blade 模板中。