Skip to content

Commit

Permalink
侧边栏链接
Browse files Browse the repository at this point in the history
  • Loading branch information
summerblue committed Mar 7, 2022
1 parent 4c616e0 commit c228771
Show file tree
Hide file tree
Showing 12 changed files with 175 additions and 7 deletions.
12 changes: 7 additions & 5 deletions app/Http/Controllers/CategoriesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@
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)
->where('category_id', $category->id)
->with('user', 'category') // 预加载防止 N+1 问题
->with('user', 'category') // 预加载防止 N+1 问题
->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'));
}
}
6 changes: 4 additions & 2 deletions app/Http/Controllers/TopicsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Support\Facades\Auth;
use App\Handlers\ImageUploadHandler;
use App\Models\User;
use App\Models\Link;

class TopicsController extends Controller
{
Expand All @@ -18,14 +19,15 @@ 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)
->with('user', 'category') // 预加载防止 N+1 问题
->paginate(20);
$active_users = $user->getActiveUsers();
$links = $link->getAllCached();

return view('topics.index', compact('topics', 'active_users'));
return view('topics.index', compact('topics', 'active_users', 'links'));
}

public function show(Request $request, Topic $topic)
Expand Down
25 changes: 25 additions & 0 deletions app/Models/Link.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Facades\Cache;

class Link extends Model
{
use HasFactory;

protected $fillable = ['title', 'link'];

public $cache_key = 'larabbs_links';
protected $cache_expire_in_seconds = 1440 * 60;

public function getAllCached()
{
// 尝试从缓存中取出 cache_key 对应的数据。如果能取到,便直接返回数据。
// 否则运行匿名函数中的代码来取出 links 表中所有的数据,返回的同时做了缓存。
return Cache::remember($this->cache_key, $this->cache_expire_in_seconds, function(){
return $this->all();
});
}
}
15 changes: 15 additions & 0 deletions app/Observers/LinkObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App\Observers;

use App\Models\Link;
use Illuminate\Support\Facades\Cache;

class LinkObserver
{
// 在保存时清空 cache_key 对应的缓存
public function saved(Link $link)
{
Cache::forget($link->cache_key);
}
}
1 change: 1 addition & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ 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);

\Illuminate\Pagination\Paginator::useBootstrap();
}
Expand Down
1 change: 1 addition & 0 deletions config/administrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
],
'站点管理' => [
'settings.site',
'links',
],
],

Expand Down
52 changes: 52 additions & 0 deletions config/administrator/links.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

use App\Models\Link;
use Illuminate\Support\Facades\Auth;

return [
'title' => '资源推荐',
'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' => '名称',
],
],
];
16 changes: 16 additions & 0 deletions database/factories/LinkFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

class LinkFactory extends Factory
{
public function definition()
{
return [
'title' => $this->faker->name,
'link' => $this->faker->url,
];
}
}
23 changes: 23 additions & 0 deletions database/migrations/2022_03_07_223007_create_links_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up()
{
Schema::create('links', function (Blueprint $table) {
$table->increments('id');
$table->string('title')->comment('资源的描述')->index();
$table->string('link')->comment('资源的链接')->index();
$table->timestamps();
});
}

public function down()
{
Schema::dropIfExists('links');
}
};
1 change: 1 addition & 0 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ public function run()
$this->call(UsersTableSeeder::class);
$this->call(TopicsTableSeeder::class);
$this->call(RepliesTableSeeder::class);
$this->call(LinksTableSeeder::class);
}
}
14 changes: 14 additions & 0 deletions database/seeders/LinksTableSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\Link;

class LinksTableSeeder extends Seeder
{
public function run()
{
Link::factory()->times(6)->create();
}
}
16 changes: 16 additions & 0 deletions resources/views/topics/_sidebar.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,19 @@
</div>
</div>
@endif

@if (count($links))
<div class="card mt-4">
<div class="card-body pt-2">
<div class="text-center mt-1 mb-0 text-muted">资源推荐</div>
<hr class="mt-2 mb-3">
@foreach ($links as $link)
<a class="d-flex mt-1 text-decoration-none" href="{{ $link->link }}">
<div class="media-body">
<span class="media-heading text-muted">{{ $link->title }}</span>
</div>
</a>
@endforeach
</div>
</div>
@endif

0 comments on commit c228771

Please sign in to comment.