laravel框架快速生成restapi

laravel框架

  1. 創(chuàng)建文章遷移表及模型
// 創(chuàng)建好了 Post 模型以及 posts 表
php artisan make:model Post -m

// 分別創(chuàng)建
// 創(chuàng)建Post 模型
php artisan make:model Post
// 創(chuàng)建posts表
php artisan make:migration create_posts_table
  1. 創(chuàng)建表的controller控制器
php artisan make:controller PostController --resource
  1. 修改api.php
Route::resource('posts', 'PostController');

路由例子

1. 注冊資源路由

// 普通注冊
Route::resource('article', 'ArticleController');
 
// 限制指定路由
Route::resource('article', 'ArticleController', ['only' => [
    'index', 'show', 'store', 'update', 'destroy'
]]);
 注意

(1)我這里是在做后臺的文章模塊,所以在Controllers下新建了一個Admin文件夾,所以新建后臺的文章模塊控制器需要加一個文件夾 “ Admin ” 。

(2)當前這個路由文件是一個自定義的admin.php路由文件,直接路由到 “/Controllers/Admin/” 文件夾下的。如何在Laravel中自定義路由文件,參考我的另一篇博文:Laravel5.5添加新路由文件并制定規(guī)則。

(3)resource路由包含多個子路由,具體參考下表:

方法  路徑  動作  路由名稱
GET /article    index   article.index
GET /article/create create  article.create
POST    /article    store   article.store
GET /article/{id}   show    article.show
GET /article/{id}/edit  edit    article.edit
PUT/PATCH   /article/{id}   update  article.update
DELETE  /article/{id}   destroy article.destroy
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容