laravel 资源控制器

Artisan 生成器来生成一个资源控制器(在之前命名后加上 --resource 选项)

php artisan make:controller PostController --resource

 

生成的控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

以上 PostController 控制器的每个方法都有对应的请求方式、路由命名、URL、方法名和业务逻辑约定。

HTTP请求方式URL控制器方法路由命名业务逻辑描述
GETpostindex()post.index展示所有文章
GETpost/createcreate()post.create发布文章表单页面
POSTpoststore()post.store获取表单提交数据并保存新文章
GETpost/{post}show()post.show展示单个文章
GETpost/{id}/editedit()post.edit编辑文章表单页面
PUTpost/{id}update()post.update获取编辑表单输入并更新文章
DELETEpost/{id}destroy()post.desc删除单个文章
Laravel 还为我们提供了一个 Route::resource 方法用于一次注册包含上面列出的所有路由,并且遵循上述所有约定:
Route::resource('post', 'PostController');
 
 

你可以通过 Artisan 命令 php artisan route:list 查看应用的所有路由:

转载于:https://www.cnblogs.com/sgm4231/p/10283279.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值