后面管理-内容管理

This commit is contained in:
fthvgb1 2018-02-17 16:40:23 +08:00
parent 873008a0ad
commit 64089902fc
6 changed files with 320 additions and 1 deletions

View File

@ -22,4 +22,54 @@ 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);
}

View File

@ -58,7 +58,14 @@ return array(
'menu' => [
'用户与权限' => [
'users',
'roles',
'permissions'
],
'内容管理' => [
'categories',
'topics',
'replies'
]
],

View File

@ -0,0 +1,54 @@
<?php
/**
* Created by PhpStorm.
* User: xing
* Date: 2018/2/17
* Time: 16:12
*/
return [
'title' => '分类',
'single' => '分类',
'model' => \App\Models\Category::class,
'action_permissions' => [
'delete' => function () {
return Auth::user()->hasRole('Founder');
}
],
'columns' => [
'id' => [
'title' => 'ID',
],
'name' => [
'title' => '名称',
'sortable' => false
],
'description' => [
'title' => '描述',
'sortable' => false,
],
'operation' => [
'title' => '管理',
'sortable' => false,
],
],
'edit_fields' => [
'name' => ['title' => '名称'],
'description' => [
'title' => '描述',
'type' => 'textarea',
]
],
'filters' => [
'id' => ['title' => '分类ID'],
'name' => ['title' => '名称'],
'description' => ['title' => '描述']
],
'rules' => [
'name' => 'required|min:1|unique:categories'
],
'messages' => [
'name.unique' => '分类名在数据库里有重复,请选用其他名称。',
'name.required' => '请确保名字至少一个字符以上'
]
];

View File

@ -0,0 +1,98 @@
<?php
/**
* Created by PhpStorm.
* User: xing
* Date: 2018/2/17
* Time: 16:26
*/
use App\Models\Reply;
return [
'title' => '回复',
'single' => '回复',
'model' => Reply::class,
'columns' => [
'id' => [
'title' => 'ID',
],
'content' => [
'title' => '内容',
'sortable' => false,
'output' => function ($value, $model) {
return '<div style="max-width:220px">' . $value . '</div>';
},
],
'user' => [
'title' => '作者',
'sortable' => false,
'output' => function ($value, $model) {
$avatar = $model->user->avatar;
$value = empty($avatar) ? 'N/A' : '<img src="' . $avatar . '" style="height:22px;width:22px"> ' . $model->user->name;
return model_link($value, $model);
},
],
'topic' => [
'title' => '话题',
'sortable' => false,
'output' => function ($value, $model) {
return '<div style="max-width:260px">' . model_admin_link($model->topic->title, $model->topic) . '</div>';
},
],
'operation' => [
'title' => '管理',
'sortable' => false,
],
],
'edit_fields' => [
'user' => [
'title' => '用户',
'type' => 'relationship',
'name_field' => 'name',
'autocomplete' => true,
'search_fields' => array("CONCAT(id, ' ', name)"),
'options_sort_field' => 'id',
],
'topic' => [
'title' => '话题',
'type' => 'relationship',
'name_field' => 'title',
'autocomplete' => true,
'search_fields' => array("CONCAT(id, ' ', title)"),
'options_sort_field' => 'id',
],
'content' => [
'title' => '回复内容',
'type' => 'textarea',
],
],
'filters' => [
'user' => [
'title' => '用户',
'type' => 'relationship',
'name_field' => 'name',
'autocomplete' => true,
'search_fields' => array("CONCAT(id, ' ', name)"),
'options_sort_field' => 'id',
],
'topic' => [
'title' => '话题',
'type' => 'relationship',
'name_field' => 'title',
'autocomplete' => true,
'search_fields' => array("CONCAT(id, ' ', title)"),
'options_sort_field' => 'id',
],
'content' => [
'title' => '回复内容',
],
],
'rules' => [
'content' => 'required'
],
'messages' => [
'content.required' => '请填写回复内容',
],
];

View File

@ -0,0 +1,110 @@
<?php
/**
* Created by PhpStorm.
* User: xing
* Date: 2018/2/17
* Time: 16:20
*/
return [
'title' => '话题',
'single' => '话题',
'model' => \App\Models\Topic::class,
'columns' => [
'id' => [
'title' => 'ID',
],
'title' => [
'title' => '话题',
'sortable' => false,
'output' => function ($value, $model) {
return '<div style="max-width:260px">' . Model_link(e($value), $model) . '</div>';
},
],
'user' => [
'title' => '作者',
'sortable' => false,
'output' => function ($value, $model) {
$avatar = $model->user->avatar;
$value = empty($avatar) ? 'N/A' : '<img src="' . $avatar . '" style="height:22px;width:22px"> ' . $model->user->name;
return model_link($value, $model);
},
],
'category' => [
'title' => '分类',
'sortable' => false,
'output' => function ($value, $model) {
return model_admin_link($model->category->name, $model->category);
},
],
'reply_count' => [
'title' => '评论',
],
'operation' => [
'title' => '管理',
'sortable' => false,
],
],
'edit_fields' => [
'title' => [
'title' => '标题',
],
'user' => [
'title' => '用户',
'type' => 'relationship',
'name_field' => 'name',
// 自动补全,对于大数据量的对应关系,推荐开启自动补全,
// 可防止一次性加载对系统造成负担
'autocomplete' => true,
// 自动补全的搜索字段
'search_fields' => ["CONCAT(id, ' ', name)"],
// 自动补全排序
'options_sort_field' => 'id',
],
'category' => [
'title' => '分类',
'type' => 'relationship',
'name_field' => 'name',
'search_fields' => ["CONCAT(id, ' ', name)"],
'options_sort_field' => 'id',
],
'reply_count' => [
'title' => '评论',
],
'view_count' => [
'title' => '查看',
],
],
'filters' => [
'id' => [
'title' => '内容 ID',
],
'user' => [
'title' => '用户',
'type' => 'relationship',
'name_field' => 'name',
'autocomplete' => true,
'search_fields' => array("CONCAT(id, ' ', name)"),
'options_sort_field' => 'id',
],
'category' => [
'title' => '分类',
'type' => 'relationship',
'name_field' => 'name',
'search_fields' => array("CONCAT(id, ' ', name)"),
'options_sort_field' => 'id',
],
],
'rules' => [
'title' => 'required'
],
'messages' => [
'title.required' => '请填写标题',
],
];

View File

@ -42,7 +42,7 @@ class UsersTableSeeder extends Seeder
$user->password = bcrypt('123456');
$user->save();
$user->assignRole('Maintainer');
$user = \App\Models\User::find(3);
$user = \App\Models\User::find(1);
$user->assignRole('Founder');
}