Skip to content

Commit

Permalink
分类列表
Browse files Browse the repository at this point in the history
  • Loading branch information
liyu001989 committed Aug 6, 2020
1 parent 0df7163 commit fe1fa8a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
16 changes: 16 additions & 0 deletions app/Http/Controllers/Api/CategoriesController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Http\Controllers\Api;

use App\Models\Category;
use Illuminate\Http\Request;
use App\Http\Resources\CategoryResource;

class CategoriesController extends Controller
{
public function index()
{
CategoryResource::wrap('data');
return CategoryResource::collection(Category::all());
}
}
19 changes: 19 additions & 0 deletions app/Http/Resources/CategoryResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class CategoryResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return parent::toArray($request);
}
}
3 changes: 3 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
// 某个用户的详情
Route::get('users/{user}', 'UsersController@show')
->name('users.show');
// 分类列表
Route::get('categories', 'CategoriesController@index')
->name('categories.index');

// 登录后可以访问的接口
Route::middleware('auth:api')->group(function() {
Expand Down

0 comments on commit fe1fa8a

Please sign in to comment.