forked from summerblue/larabbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.php
47 lines (36 loc) · 1.31 KB
/
helpers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
function route_class()
{
return str_replace('.', '-', Route::currentRouteName());
}
function make_excerpt($value, $length = 200)
{
$excerpt = trim(preg_replace('/\r\n|\r|\n+/', ' ', strip_tags($value)));
return str_limit($excerpt, $length);
}
function model_admin_link($title, $model)
{
return model_link($title, $model, 'admin');
}
function model_link($title, $model, $prefix = '')
{
// 获取数据模型的复数蛇形命名
$model_name = model_plural_name($model);
// 初始化前缀
$prefix = $prefix ? "/$prefix/" : '/';
// 使用站点 URL 拼接全量 URL
$url = config('app.url') . $prefix . $model_name . '/' . $model->id;
// 拼接 HTML A 标签,并返回
return '<a href="' . $url . '" target="_blank">' . $title . '</a>';
}
function model_plural_name($model)
{
// 从实体中获取完整类名,例如:App\Models\User
$full_class_name = get_class($model);
// 获取基础类名,例如:传参 `App\Models\User` 会得到 `User`
$class_name = class_basename($full_class_name);
// 蛇形命名,例如:传参 `User` 会得到 `user`, `FooBar` 会得到 `foo_bar`
$snake_case_name = snake_case($class_name);
// 获取子串的复数形式,例如:传参 `user` 会得到 `users`
return str_plural($snake_case_name);
}