Skip to content

Commit

Permalink
侧边栏链接
Browse files Browse the repository at this point in the history
  • Loading branch information
summerblue committed Sep 20, 2019
1 parent 10d4e19 commit 5f8215f
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 8 deletions.
10 changes: 6 additions & 4 deletions app/Http/Controllers/CategoriesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@
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)
->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'));
}
}
7 changes: 5 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 App\Models\User;
use Auth;
use App\Handlers\ImageUploadHandler;
use App\Models\Link;

class TopicsController extends Controller
{
Expand All @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions app/Models/Link.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Cache;

class Link extends Model
{
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 Cache;

class LinkObserver
{
// 在保存时清空 cache_key 对应的缓存
public function saved(Link $link)
{
Cache::forget($link->cache_key);
}
}
3 changes: 1 addition & 2 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
1 change: 1 addition & 0 deletions config/administrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
],
'站点管理' => [
'settings.site',
'links',
],
],

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

use App\Models\Link;

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

use Faker\Generator as Faker;

$factory->define(App\Models\Link::class, function (Faker $faker) {
return [
'title' => $faker->name,
'link' => $faker->url,
];
});
23 changes: 23 additions & 0 deletions database/migrations/2019_09_20_121925_create_links_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

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

class CreateLinksTable 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/seeds/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ public function run()
$this->call(UsersTableSeeder::class);
$this->call(TopicsTableSeeder::class);
$this->call(RepliesTableSeeder::class);
$this->call(LinksTableSeeder::class);
}
}
16 changes: 16 additions & 0 deletions database/seeds/LinksTableSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

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

class LinksTableSeeder extends Seeder
{
public function run()
{
// 生成数据集合
$links = factory(Link::class)->times(6)->make();

// 将数据集合转换为数组,并插入到数据库中
Link::insert($links->toArray());
}
}
15 changes: 15 additions & 0 deletions resources/views/topics/_sidebar.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,18 @@
</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="media mt-1" 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 5f8215f

Please sign in to comment.