From 66a176b5921c144de324211657f3190125c53a83 Mon Sep 17 00:00:00 2001 From: fthvgb1 Date: Sun, 10 Jun 2018 18:35:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=AF=9D=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/Api/TopicsController.php | 13 +++++++++++ app/Http/Requests/Api/TopicRequest.php | 23 +++++++++++++++---- app/Providers/AppServiceProvider.php | 10 ++++++++ routes/api.php | 6 +++-- 4 files changed, 45 insertions(+), 7 deletions(-) diff --git a/app/Http/Controllers/Api/TopicsController.php b/app/Http/Controllers/Api/TopicsController.php index a8d49a5..6bf7174 100644 --- a/app/Http/Controllers/Api/TopicsController.php +++ b/app/Http/Controllers/Api/TopicsController.php @@ -15,4 +15,17 @@ class TopicsController extends Controller $topic->save(); return $this->response->item($topic, new TopicTransformer())->setStatusCode(201); } + + /** + * @param TopicRequest $request + * @param Topic $topic + * @return \Dingo\Api\Http\Response + * @throws \Illuminate\Auth\Access\AuthorizationException + */ + public function update(TopicRequest $request, Topic $topic) + { + $this->authorize('update', $topic); + $topic->update($request->all()); + return $this->response->item($topic, new TopicTransformer()); + } } diff --git a/app/Http/Requests/Api/TopicRequest.php b/app/Http/Requests/Api/TopicRequest.php index 8345782..5c1bcd1 100644 --- a/app/Http/Requests/Api/TopicRequest.php +++ b/app/Http/Requests/Api/TopicRequest.php @@ -25,11 +25,24 @@ class TopicRequest extends FormRequest public function rules() { - return [ - 'title' => 'required|string', - 'body' => 'required|string', - 'category_id' => 'required|exists:categories,id', - ]; + switch ($this->method()) { + case 'POSt': + return [ + 'title' => 'required|string', + 'body' => 'required|string', + 'category_id' => 'required|exists:categories,id', + ]; + case 'PATCH': + return [ + 'title' => 'string', + 'body' => 'string', + 'category' => 'exists:categories,id' + ]; + default: + return []; + } + + } public function attributes() diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 4802275..dca12d7 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -9,6 +9,9 @@ use App\Observers\LinkObserver; use App\Observers\ReplyObserver; use App\Observers\TopicObserver; use Carbon\Carbon; +use Dingo\Api\Facade\API; +use Illuminate\Auth\Access\AuthorizationException; +use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider @@ -39,5 +42,12 @@ class AppServiceProvider extends ServiceProvider if (app()->isLocal()) { $this->app->register(\VIACreative\SudoSu\ServiceProvider::class); } + + API::error(function (ModelNotFoundException $exception) { + abort(404); + }); + API::error(function (AuthorizationException $exception) { + abort(403, $exception->getMessage()); + }); } } diff --git a/routes/api.php b/routes/api.php index 27006ac..76a8273 100644 --- a/routes/api.php +++ b/routes/api.php @@ -15,7 +15,7 @@ $api = app('Dingo\Api\Routing\Router'); $api->version('v1', [ 'namespace' => 'App\Http\Controllers\Api', - 'middleware' => 'serializer:array', + 'middleware' => ['serializer:array', 'bindings'], ], function ($api) { $api->group([ 'middleware' => 'api.throttle', @@ -54,7 +54,7 @@ $api->version('v1', [ }); // 需要 token 验证的接口 - $api->group(['middleware' => 'api.auth'], function ($api) { + $api->group(['middleware' => 'api.auth',], function ($api) { // 当前登录用户信息 $api->get('user', 'UsersController@me') ->name('api.user.show'); @@ -66,6 +66,8 @@ $api->version('v1', [ ->name('api.user.update'); //发布话题 $api->post('topics', 'TopicsController@store')->name('api.topics.store'); + //发布话题 + $api->patch('topics/{topic}', 'TopicsController@update')->name('api.topics.update'); }); });