Githubにあるサンプルを見ながら掲示板の開発を行っております。
不明点があり、どなたかご教示頂ければ幸いです。
下記のように、作者はController→Serviceクラス→repositoryクラスへ処理を流しておられます。
質問したい部分は、controllerでdestroyの処理を行う際、リポジトリクラスのdeleteCommentを呼び出し→
deleteCommentは更に同リポジトリクラスのgetCommentByIdを呼び出し、となっていますが、この引数である"id"は、どうしてdeleteCommentの中のgetCommentByIdでも使うことができるのか、ということです。
私の考えではdeleteComment→getCommentByIdを呼び出した際、引数には何も渡って来ず、Call to a member function delete() on nullとなるのではと考えております。
よろしくお願いいたします。
php
1<?php namespace Owl\Repositories\Eloquent; 2 3use Owl\Repositories\CommentRepositoryInterface; 4use Owl\Repositories\Eloquent\Models\Comment; 5 6class CommentRepository implements CommentRepositoryInterface 7{ 8 protected $comment; 9 10 public function __construct(Comment $comment) 11 { 12 $this->comment = $comment; 13 } 14 15 /** 16 * Delete a comment. 17 * 18 * @param $id int 19 * @return boolean 20 */ 21 public function deleteComment($id) 22 { 23 $comment = $this->getCommentById($id); 24 $comment->delete(); 25 26 return $comment; 27 } 28 29 /** 30 * Get a comment by comment id. 31 * 32 * @param $id int 33 * @return Illuminate\Database\Eloquent\Model 34 */ 35 public function getCommentById($id) 36 { 37 $comment = $this->comment->with('user')->find($id); 38 if (!empty($comment)) { 39 return $comment; 40 } 41 return false; 42 } 43}
php
1<?php namespace Owl\Http\Controllers; 2 3use Illuminate\Contracts\Events\Dispatcher; 4use Owl\Services\UserService; 5use Owl\Services\ItemService; 6use Owl\Services\CommentService; 7use Owl\Events\Item\CommentEvent; 8 9class CommentController extends Controller 10{ 11 protected $userService; 12 protected $itemService; 13 protected $commentService; 14 private $status = 400; 15 16 public function __construct( 17 UserService $userService, 18 ItemService $itemService, 19 CommentService $commentService 20 ) { 21 $this->userService = $userService; 22 $this->itemService = $itemService; 23 $this->commentService = $commentService; 24 } 25 26 27 28 public function destroy() 29 { 30 if ($comment = $this->commentService->getCommentById(\Input::get('id'))) { 31 $this->commentService->deleteComment($comment->id); 32 $this->status = 200; 33 } 34 return \Response::make("", $this->status); 35 } 36} 37
php
1<?php namespace Owl\Services; 2 3use Owl\Repositories\CommentRepositoryInterface; 4 5class CommentService extends Service 6{ 7 protected $commentRepo; 8 9 public function __construct( 10 CommentRepositoryInterface $commentRepo 11 ) { 12 $this->commentRepo = $commentRepo; 13 } 14 15 /** 16 * Delete a comment. 17 * 18 * @param $id int 19 * @return boolean 20 */ 21 public function deleteComment($id) 22 { 23 return $this->commentRepo->deleteComment($id); 24 } 25 26 /** 27 * Get a comment by comment id. 28 * 29 * @param $id int 30 * @return Illuminate\Database\Eloquent\Model 31 */ 32 public function getCommentById($id) 33 { 34 return $this->commentRepo->getCommentById($id); 35 } 36} 37
回答1件
あなたの回答
tips
プレビュー