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 fe1fa8a commit 727a6c8
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
20 changes: 20 additions & 0 deletions app/Http/Controllers/Api/TopicsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Http\Controllers\Api;

use App\Models\Topic;
use Illuminate\Http\Request;
use App\Http\Resources\TopicResource;
use App\Http\Requests\Api\TopicRequest;

class TopicsController extends Controller
{
public function store(TopicRequest $request, Topic $topic)
{
$topic->fill($request->all());
$topic->user_id = $request->user()->id;
$topic->save();

return new TopicResource($topic);
}
}
24 changes: 24 additions & 0 deletions app/Http/Requests/Api/TopicRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Http\Requests\Api;

class TopicRequest extends FormRequest
{
public function rules()
{
return [
'title' => 'required|string',
'body' => 'required|string',
'category_id' => 'required|exists:categories,id',
];
}

public function attributes()
{
return [
'title' => '标题',
'body' => '话题内容',
'category_id' => '分类',
];
}
}
19 changes: 19 additions & 0 deletions app/Http/Resources/TopicResource.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 TopicResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return parent::toArray($request);
}
}
8 changes: 8 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
// 分类列表
Route::get('categories', 'CategoriesController@index')
->name('categories.index');
// 话题列表,详情
Route::resource('topics', 'TopicsController')->only([
'index', 'show'
]);

// 登录后可以访问的接口
Route::middleware('auth:api')->group(function() {
Expand All @@ -69,6 +73,10 @@
// 上传图片
Route::post('images', 'ImagesController@store')
->name('images.store');
// 发布话题
Route::resource('topics', 'TopicsController')->only([
'store', 'update', 'destroy'
]);
});
});
});

0 comments on commit 727a6c8

Please sign in to comment.