<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Cache;
use App\Models\Post;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class PostController extends Controller
{
/**
* 显示文章列表.
*
* @return Response
*/
public function index()
{
//使用all获取所有数据,如果数据量大采用分页获取
$posts = Post::all();
if(!$posts)
exit('还没有发布任何文章!');
$html = '<ul>';
foreach ($posts as $post) {
$html .= '<li><a href='.route('post.show',['post'=>$post]).'>'.$post->title.'</li>';
}
$html .= '</ul>';
return $html;
}
/**
* 创建新文章表单页面
*
* @return Response
*/
public function create()
{
$postUrl = route('post.store');
$csrf_field = csrf_field();
$html = <<<CREATE
<form action="$postUrl" method="POST">
$csrf_field
<input type="text" name="title"><br/><br/>
<textarea name="content" cols="50" rows="5"></textarea><br/><br/>
<input type="submit" value="提交"/>
</form>
CREATE;
return $html;
}
/**
* 将新创建的文章存储到存储器
*
* @param Request $request
* @return Response
*/
public function store(Request $request)
{
$title = $request->input('title');
$content = $request->input('content');
$post = new Post;
$post->title = $title;
$post->content = $content;
$post->save();
return redirect()->route('post.show',['post'=>$post]);
}
/**
* 显示指定文章
*
* @param int $id
* @return Response
*/
public function show($id)
{
$post = Cache::get('post_'.$id);
if(!$post){
$post = Post::find($id);
if(!$post)
exit('指定文章不存在!');
Cache::put('post_'.$id,$post,60*24*7);
}
if(!Cache::get('post_views_'.$id))
Cache::forever('post_views_'.$id,0);
$views = Cache::increment('post_views_'.$id);
Cache::forever('post_views_'.$id,$views);
$editUrl = route('post.edit',['post'=>$post]);
$deleteUrl = route('post.destroy',['post'=>$post]);
$html = <<<POST
<h3>{$post->title}</h3>
<p>{$post->content}</p>
<i>已有{$views}人阅读</i>
<p>
<a href="{$editUrl}">编辑</a>
</p>
POST;
return $html;
}
/**
* 显示编辑指定文章的表单页面
*
* @param int $id
* @return Response
*/
public function edit($id)
{
$post = Post::find($id);
if(!$post)
exit('指定文章不存在!');
$postUrl = route('post.update',['post'=>$post]);
$csrf_field = csrf_field();
$html = <<<CREATE
<form action="$postUrl" method="POST">
$csrf_field
<input type="hidden" name="_method" value="PUT"/>
<input type="text" name="title" value="{$post->title}"><br/><br/>
<textarea name="content" cols="50" rows="5">{$post->content}</textarea><br/><br/>
<input type="submit" value="提交"/>
</form>
CREATE;
return $html;
}
/**
* 在存储器中更新指定文章
*
* @param Request $request
* @param int $id
* @return Response
*/
public function update(Request $request, $id)
{
$post = Post::find($id);
if(!$post)
exit('指定文章不存在!');
$title = $request->input('title');
$content = $request->input('content');
$post->title = $title;
$post->content = $content;
$post->save();
return redirect()->route('post.show',['post'=>$post]);
}
/**
* 从存储器中移除指定文章
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
$post = Post::find($id);
if(!$post)
exit('指定被删除文章不存在!');
if($post->delete()){
redirect()->route('post.index');
}else{
exit('删除文章失败!');
}
}
}
//保存之后更新缓存数据
Post::saved(function($post){
$cacheKey = 'post_'.$post->id;
$cacheData = Cache::get($cacheKey);
if(!$cacheData){
Cache::add($cacheKey,$post,60*24*7);
}else{
Cache::put($cacheKey,$post,60*24*7);
}
});
//删除之后清除缓存数据
Post::deleted(function($post){
$cacheKey = 'post_'.$post->id;
$cacheData = Cache::get($cacheKey);
if($cacheData){
Cache::forget($cacheKey);
}
if(Cache::get('post_views_'.$post->id))
Cache::forget('post_views_'.$post->id);
});
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有