laravel-learn-bbs/app/helpers.php

75 lines
1.7 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* Created by PhpStorm.
* User: xing
* Date: 2017/12/30
* Time: 22:23
*/
function route_class()
{
return str_replace('.', '-', \Illuminate\Support\Facades\Route::currentRouteName());
}
/**
* seo
* @param $value
* @param int $length
* @return string
*/
function make_excerpt($value, $length = 200)
{
$excerpt = trim(preg_replace('/\r\n|\r|\n+/', ' ', strip_tags($value)));
return str_limit($excerpt, $length);
}
/**
* @param $title
* @param $model
* @return string
*/
function model_admin_link($title, $model)
{
return model_link($title, $model, 'admin');
}
/**
* @param $title
* @param $model
* @param string $prefix
* @return string
*/
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>';
}
/**
* @param $model
* @return string
*/
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);
}