-
创建postController.php文件,可以用命令行,
详情可以参考https://laravelacademy.org/post/549.html/page/2#comments
感谢laravel学院[/撒花]
这里只完善删除功能(用伪造表单):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() { //从缓存中获取缓存项,第二个参数为缓存项不存在时返回的自定义默认值 $posts = Cache::get('posts',[]); if(!$posts) exit('Nothing'); $csrf_field = csrf_field(); $html = '<ul>'; foreach ($posts as $key=>$post) { $html .= '<li><a href='.route('post.show',['post'=>$key]).'>'.$post['title'].'</li>'; $html .= '<form action='.route('post.show',['post'=>$key]).' method="POST"> '.$csrf_field.' <input type="hidden" name="_method" value="DELETE"/> <input type="submit" value="删除"/> </form>'; } $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 = ['title'=>trim($title),'content'=>trim($content)];//trim 从字符串的开头和结尾去掉空格 $posts = Cache::get('posts',[]); if(!Cache::get('post_id')){ Cache::add('post_id',1,60); }else{ Cache::increment('post_id',1); } $posts[Cache::get('post_id')] = $post; //存储缓存项,要指定数据被缓存的时间 //Cache::put('key', 'value', $expiresAt); Cache::put('posts',$posts,60); return redirect()->route('post.show',['post'=>Cache::get('post_id')]); } /** * 显示指定文章 * * @param int $id * @return Response */ public function show($id) { // $posts = Cache::get('posts',[]); if(!$posts || !$posts[$id]) exit('Nothing Found!'); $post = $posts[$id]; $editUrl = route('post.edit',['post'=>$id]); $html = <<<DETAIL <h3>{$post['title']}</h3> <p>{$post['content']}</p> <p> <a href="{$editUrl}">编辑</a> </p> DETAIL; return $html; } /** * 显示编辑指定文章的表单页面 * * @param int $id * @return Response */ public function edit($id) { // $posts = Cache::get('posts',[]); if(!$posts || !$posts[$id]) exit('Nothing Found!'); $post = $posts[$id]; $postUrl = route('post.update',['post'=>$id]); $csrf_field = csrf_field(); $html = <<<UPDATE <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> UPDATE; return $html; } /** * 在存储器中更新指定文章 * * @param Request $request * @param int $id * @return Response */ public function update(Request $request, $id) { // $posts = Cache::get('posts',[]); if(!$posts || !$posts[$id]) exit('Nothing Found!'); $post = $posts[$id]; $title = $request->input('title'); $content = $request->input('content'); $posts[$id]['title'] = trim($title); $posts[$id]['content'] = trim($content); Cache::put('posts',$posts,60); return redirect()->route('post.index'); } /** * 从存储器中移除指定文章 * * @param int $id * @return Response */ public function destroy($id) { // $posts = Cache::get('posts',[]); if(!$posts || !$posts[$id]) exit('Nothing Deleted!'); unset($posts[$id]); //将删除后的内容,重写如缓存中,替换原来内容 Cache::put('posts',$posts,60); Cache::decrement('post_id',1); return redirect()->route('post.index'); } }
-
记得添加路由:在route.php文件添加
// 为RESTFul风格控制器注册路由
Route::resource(‘post’,‘PostController’);/** * GET /post index post.index * GET /post/create create post.create * POST /post store post.store * GET /post/{post} show post.show * GET /post/{post}/edit edit post.edit * PUT/PATCH /post/{post} update post.update * DELETE /post/{post} destroy post.destroy */
3.记得加上 use Cache 类哦~~