laravel框架
- 創(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
- 創(chuàng)建表的controller控制器
php artisan make:controller PostController --resource
- 修改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