Laravel

来源:互联网 发布:淘宝设置手机专享价 编辑:程序博客网 时间:2024/05/16 09:56

Laravel有很多全局函数来帮助我们更高效地编程,这些函数都是我们经常可能会使用到的:

Available Methods

Arrays

array_add array_collapse array_divide array_dot array_except
array_first array_flatten array_forget array_get array_has array_last
array_only array_pluck array_prepend array_pull array_set array_sort
array_sort_recursive array_where array_wrap head last

Paths

app_path base_path config_path database_path mix public_path
resource_path storage_path

Strings

camel_case class_basename e ends_with kebab_case snake_case str_limit
starts_with str_contains str_finish str_is str_plural str_random
str_singular str_slug studly_case title_case trans trans_choice

URLs

action asset secure_asset route secure_url url

Miscellaneous

abort abort_if abort_unless auth back bcrypt cache collect config
csrf_field csrf_token dd dispatch env event factory info logger
method_field old redirect request response retry session value view

今天我们来看一下操作URL的一些函数的作用。
比较常见的有route()函数,这个函数为一个已命名的route生成URL:

$url = route('my-route-name');

如果route带有参数,那么我们可以把参数传递进route()的第二个参数里:

$url = route('my-route-name', ['id' => 1]);

另外一个很常见的就是url()函数。这个函数可以返回给定路径的完整URL:

//假设我们的网址为 http://blog.csdn.net///以下函数返回 http://blog.csdn.net/user/profileecho url('user/profile');//返回 http://blog.csdn.net/user/profile/2echo url('user/profile', [2]);

如果我们不给url()函数传递参数,那么将返回一个Illuminate\Routing\UrlGenerator对象:

//返回当前页面的地址echo url()->current();//返回当前页面的完整路径echo url()->full();//返回前一个页面的地址echo url()->previous();

所以,如果我们的view中想定义一个返回按钮,可以这样定义:

<!-- 注意我们使用的是blade template (my-view.blade.php) --><a href="{{ url()->previous() }}" class="btn">返回</a>

这样每当我们访问当前view页面时,该链接会自动生成前一个页面的地址,是不是很方便呢!

0 0
原创粉丝点击