File tree Expand file tree Collapse file tree 3 files changed +38
-0
lines changed
Expand file tree Collapse file tree 3 files changed +38
-0
lines changed Original file line number Diff line number Diff line change 22
33namespace App \Http \Controllers \API ;
44
5+ use App \Http \Resources \PostResource ;
56use App \Models \Post ;
67use Illuminate \Http \Request ;
78use App \Http \Controllers \Controller ;
@@ -28,4 +29,10 @@ public function index(Request $request)
2829 ];
2930 return response ()->json ($ data );
3031 }
32+
33+ public function detail ($ id )
34+ {
35+ $ post = Post::findOrFail ($ id );
36+ return new PostResource ($ post );
37+ }
3138}
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace App \Http \Resources ;
4+
5+ use Illuminate \Http \Resources \Json \JsonResource ;
6+
7+ class PostResource extends JsonResource
8+ {
9+ /**
10+ * Transform the resource into an array.
11+ *
12+ * @param \Illuminate\Http\Request $request
13+ * @return array
14+ */
15+ public function toArray ($ request )
16+ {
17+ return [
18+ 'id ' => $ this ->id ,
19+ 'title ' => $ this ->title ,
20+ 'image ' => url (config ('blog.uploads.webpath ' ) . '/ ' . $ this ->page_image ),
21+ 'content ' => $ this ->content_html ,
22+ 'author ' => '学院君 ' ,
23+ 'posted_at ' => $ this ->publish_date ,
24+ 'views ' => mt_rand (1 , 100000 ),
25+ 'votes ' => mt_rand (1 , 1000 )
26+ ];
27+ }
28+ }
Original file line number Diff line number Diff line change 1313|
1414*/
1515Route::middleware ('throttle:60,1 ' )->prefix ('v1 ' )->group (function () {
16+ // 文章首页
1617 Route::get ('/articles ' , 'API\PostController@index ' );
18+ // 文章详情页
19+ Route::get ('/article/{id} ' , 'API\PostController@detail ' )->where (['id ' => '[1-9]{1}[0-9]* ' ]);
1720});
1821
1922Route::middleware ('auth:api ' )->get ('/user ' , function (Request $ request ) {
You can’t perform that action at this time.
0 commit comments