本文介绍了在Laravel 5中,{{url}}和{{asset}}有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

例如,

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

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

这两种形式都可以.

那么,有什么区别?

推荐答案

确定要使用哪个 URL帮助器

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

Deciding which URL helper to use

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('search') }}
// http://www.example.com/search

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

  • 用于直接提供服务的文件,例如CSS,图像,javascript.
  • 仅接受直接路径.

  • 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

  • 用于每条路线(应命名每条路线,以帮助将来的路径更改).
  • 需要命名路由.
  • 接受路由参数的关联数组.
  • 允许相对路径和绝对路径优先(默认).

  • 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

这篇关于在Laravel 5中,{{url}}和{{asset}}有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 10:07