From 5f8215f686b4a659e4c4874149a01960d929b738 Mon Sep 17 00:00:00 2001 From: Summer Date: Fri, 20 Sep 2019 04:24:21 +0000 Subject: [PATCH] =?UTF-8?q?=E4=BE=A7=E8=BE=B9=E6=A0=8F=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/CategoriesController.php | 10 ++-- app/Http/Controllers/TopicsController.php | 7 ++- app/Models/Link.php | 23 +++++++++ app/Observers/LinkObserver.php | 15 ++++++ app/Providers/AppServiceProvider.php | 3 +- config/administrator.php | 1 + config/administrator/links.php | 51 +++++++++++++++++++ database/factories/LinkFactory.php | 10 ++++ .../2019_09_20_121925_create_links_table.php | 23 +++++++++ database/seeds/DatabaseSeeder.php | 1 + database/seeds/LinksTableSeeder.php | 16 ++++++ resources/views/topics/_sidebar.blade.php | 15 ++++++ 12 files changed, 167 insertions(+), 8 deletions(-) create mode 100644 app/Models/Link.php create mode 100644 app/Observers/LinkObserver.php create mode 100644 config/administrator/links.php create mode 100644 database/factories/LinkFactory.php create mode 100644 database/migrations/2019_09_20_121925_create_links_table.php create mode 100644 database/seeds/LinksTableSeeder.php diff --git a/app/Http/Controllers/CategoriesController.php b/app/Http/Controllers/CategoriesController.php index 054d8f85e..c8d842484 100644 --- a/app/Http/Controllers/CategoriesController.php +++ b/app/Http/Controllers/CategoriesController.php @@ -6,10 +6,11 @@ use App\Models\Topic; use App\Models\Category; use App\Models\User; +use App\Models\Link; class CategoriesController extends Controller { - public function show(Category $category, Request $request, Topic $topic, User $user) + public function show(Category $category, Request $request, Topic $topic, User $user, Link $link) { // 读取分类 ID 关联的话题,并按每 20 条分页 $topics = $topic->withOrder($request->order) @@ -17,8 +18,9 @@ public function show(Category $category, Request $request, Topic $topic, User $u ->paginate(20); // 活跃用户列表 $active_users = $user->getActiveUsers(); - - // 传参变量话题和分类到模板中 - return view('topics.index', compact('topics', 'category', 'active_users')); + // 资源链接 + $links = $link->getAllCached(); + // 传参变量到模板中 + return view('topics.index', compact('topics', 'category', 'active_users', 'links')); } } diff --git a/app/Http/Controllers/TopicsController.php b/app/Http/Controllers/TopicsController.php index 2eb533c05..9991c3187 100644 --- a/app/Http/Controllers/TopicsController.php +++ b/app/Http/Controllers/TopicsController.php @@ -10,6 +10,7 @@ use App\Models\User; use Auth; use App\Handlers\ImageUploadHandler; +use App\Models\Link; class TopicsController extends Controller { @@ -18,11 +19,13 @@ public function __construct() $this->middleware('auth', ['except' => ['index', 'show']]); } - public function index(Request $request, Topic $topic, User $user) + public function index(Request $request, Topic $topic, User $user, Link $link) { $topics = $topic->withOrder($request->order)->paginate(20); $active_users = $user->getActiveUsers(); - return view('topics.index', compact('topics', 'active_users')); + $links = $link->getAllCached(); + + return view('topics.index', compact('topics', 'active_users', 'links')); } public function show(Request $request, Topic $topic) diff --git a/app/Models/Link.php b/app/Models/Link.php new file mode 100644 index 000000000..cfe9453c5 --- /dev/null +++ b/app/Models/Link.php @@ -0,0 +1,23 @@ +cache_key, $this->cache_expire_in_seconds, function(){ + return $this->all(); + }); + } +} diff --git a/app/Observers/LinkObserver.php b/app/Observers/LinkObserver.php new file mode 100644 index 000000000..191536fc2 --- /dev/null +++ b/app/Observers/LinkObserver.php @@ -0,0 +1,15 @@ +cache_key); + } +} diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index e70be287a..97d139c41 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -28,7 +28,6 @@ public function boot() \App\Models\User::observe(\App\Observers\UserObserver::class); \App\Models\Reply::observe(\App\Observers\ReplyObserver::class); \App\Models\Topic::observe(\App\Observers\TopicObserver::class); - - // + \App\Models\Link::observe(\App\Observers\LinkObserver::class); } } diff --git a/config/administrator.php b/config/administrator.php index 1fe4e39a1..3f829db6d 100644 --- a/config/administrator.php +++ b/config/administrator.php @@ -49,6 +49,7 @@ ], '站点管理' => [ 'settings.site', + 'links', ], ], diff --git a/config/administrator/links.php b/config/administrator/links.php new file mode 100644 index 000000000..c27ece078 --- /dev/null +++ b/config/administrator/links.php @@ -0,0 +1,51 @@ + '资源推荐', + 'single' => '资源推荐', + + 'model' => Link::class, + + // 访问权限判断 + 'permission'=> function() + { + // 只允许站长管理资源推荐链接 + return Auth::user()->hasRole('Founder'); + }, + + 'columns' => [ + 'id' => [ + 'title' => 'ID', + ], + 'title' => [ + 'title' => '名称', + 'sortable' => false, + ], + 'link' => [ + 'title' => '链接', + 'sortable' => false, + ], + 'operation' => [ + 'title' => '管理', + 'sortable' => false, + ], + ], + 'edit_fields' => [ + 'title' => [ + 'title' => '名称', + ], + 'link' => [ + 'title' => '链接', + ], + ], + 'filters' => [ + 'id' => [ + 'title' => '标签 ID', + ], + 'title' => [ + 'title' => '名称', + ], + ], +]; diff --git a/database/factories/LinkFactory.php b/database/factories/LinkFactory.php new file mode 100644 index 000000000..7881f8212 --- /dev/null +++ b/database/factories/LinkFactory.php @@ -0,0 +1,10 @@ +define(App\Models\Link::class, function (Faker $faker) { + return [ + 'title' => $faker->name, + 'link' => $faker->url, + ]; +}); diff --git a/database/migrations/2019_09_20_121925_create_links_table.php b/database/migrations/2019_09_20_121925_create_links_table.php new file mode 100644 index 000000000..86b909588 --- /dev/null +++ b/database/migrations/2019_09_20_121925_create_links_table.php @@ -0,0 +1,23 @@ +increments('id'); + $table->string('title')->comment('资源的描述')->index(); + $table->string('link')->comment('资源的链接')->index(); + $table->timestamps(); + }); + } + + public function down() + { + Schema::dropIfExists('links'); + } +} diff --git a/database/seeds/DatabaseSeeder.php b/database/seeds/DatabaseSeeder.php index 285d2de68..c70efffe0 100644 --- a/database/seeds/DatabaseSeeder.php +++ b/database/seeds/DatabaseSeeder.php @@ -14,5 +14,6 @@ public function run() $this->call(UsersTableSeeder::class); $this->call(TopicsTableSeeder::class); $this->call(RepliesTableSeeder::class); + $this->call(LinksTableSeeder::class); } } diff --git a/database/seeds/LinksTableSeeder.php b/database/seeds/LinksTableSeeder.php new file mode 100644 index 000000000..e4af6a227 --- /dev/null +++ b/database/seeds/LinksTableSeeder.php @@ -0,0 +1,16 @@ +times(6)->make(); + + // 将数据集合转换为数组,并插入到数据库中 + Link::insert($links->toArray()); + } +} diff --git a/resources/views/topics/_sidebar.blade.php b/resources/views/topics/_sidebar.blade.php index 78234b328..05b9be1ba 100644 --- a/resources/views/topics/_sidebar.blade.php +++ b/resources/views/topics/_sidebar.blade.php @@ -24,3 +24,18 @@ @endif +@if (count($links)) +
+
+
资源推荐
+
+ @foreach ($links as $link) + +
+ {{ $link->title }} +
+
+ @endforeach +
+
+@endif