laravel_基础_简单博客_RESTFul风格控制器(resource)

15 篇文章 0 订阅
1. 创建控制器
php artisan make:controller PostController
2. 控制器里方法:
<?php

    namespace App\Http\Controllers;

    use Illuminate\Http\Request;
    use App\Http\Requests;

    class PostController extends Controller
    {
        /**
         * 显示文章列表.
         *
         * @return Response
         */
        public function index()
        {
            //
        }

        /**
         * 创建新文章表单页面
         *
         * @return Response
         */
        public function create()
        {
            //
        }

        /**
         * 将新创建的文章存储到存储器
         *
         * @param Request $request
         * @return Response
         */
        public function store(Request $request)
        {
            //
        }

        /**
         * 显示指定文章
         *
         * @param int $id
         * @return Response
         */
        public function show($id)
        {
            //
        }

        /**
         * 显示编辑指定文章的表单页面
         *
         * @param int $id
         * @return Response
         */
        public function edit($id)
        {
            //
        }

        /**
         * 在存储器中更新指定文章
         *
         * @param Request $request
         * @param int $id
         * @return Response
         */
        public function update(Request $request, $id)
        {
            //
        }

        /**
         * 从存储器中移除指定文章
         *
         * @param int $id
         * @return Response
         */
        public function destroy($id)
        {
            //
        }
    }

【注意】:这几个方法是resource控制器基本方法,规定这么写,呵呵。。。

2.为控制器注册resource路由

routes.php中添加:

Route::resource('post','PostController');

该路由自动包涵多个子路由:

方法路径动作路由名称
GET/postindexpost.index
GET/post/createcreatepost.create
POST/poststorepost.store
GET/post/{post}showpost.show
GET/post/{post}/editeditpost.edit
PUT/PATCH/post/{post}updatepost.update
DELETE/post/{post}destroypost.destroy

GET方式访问http://dev.mylaravel.com/post,则访问的是PostControllerindex方法,可以通过route('post.index')生成URL
POST方式访问http://dev.mylaravel.com/post,则访问的是PostControllerstore方法,可以通过route('post.store')来生成URL。

3. 控制器方法填充,实现增删改查
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use Cache;

class PostController extends Controller
{
    /**
     * 显示文章列表.
     *
     * @return Response
     */
    public function index()
    {
        $posts = Cache::get('posts',[]);
        if(!$posts)
            exit('Nothing');
        $html = '<ol>';
        $html .= '<h2>帖子列表</h2>';
        $html .= '<a href='.route('post.create').'>发帖</a></h2>';

        foreach ($posts as $key=>$post) {
            $html .= '<li>标题:';
            $html .= '<a href="'.route('post.show',['post'=>$key]).'">'.$post['title'].'</a> ';
            $html .= '<a href="'.route('post.destroy',['post'=>$key]).'"><font color="red">[删除]</font></a>';
            $html .= '</li>';
        }

        $html .= '</ol>';

        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>
        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)];

        $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('posts',$posts,60);
        // return redirect()->route('post.show',['post'=>Cache::get('post_id')]);
        return redirect()->route('post.index');
    }

    /**
     * 显示指定文章
     *
     * @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]);
        $indexUrl = route('post.index');
        $html = <<<DETAIL
            <h3>{$post['title']}</h3>
            <p>{$post['content']}</p>
            <p>
                <a href="{$editUrl}">编辑</a>
                <a href="{$indexUrl}">返回列表</a>
            </p>

        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>
        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!');

        $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.show',['post'=>Cache::get('post_id')]);
    }

    /**
     * 从存储器中移除指定文章
     *
     * @param int $id
     * @return Response
     */
    public function destroy($id)
    {
        $posts = Cache::get('posts',[]);
        if(!$posts || !$posts[$id])
            exit('Nothing Deleted!');

        unset($posts[$id]);
        Cache::decrement('post_id',1);

        return redirect()->route('post.index');
    }
}
4. 使用

现在简单博客系统已经写好了,打开浏览器

get方式访问'/post/create':显示创建文章表单页面
get方式访问'/post':显示文章列表页面

【注意】:要删除文章,需要参考编辑表单伪造删除表单方法为DELETE(一般使用AJAX删除),这里没有用到视图,暂时不做演示。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值