zarcel
zarcel

Reputation: 1241

How to design controllers communication

Lets say I have UserControler that handles user creation deletion etc and Comments conntroler that handles comment's adding deleting modyfing etc.

What If my user wants to add a comment? Should the userController have addComment method? Or should I handle this in commentsController(if so how do I pass user data)? Maybe I don't need commentsController at all?

How do I design it properly according to MVC(I am using laravel)?

Upvotes: 0

Views: 261

Answers (3)

user1669496
user1669496

Reputation: 33148

In a case like this where each comment belongs to only one user, I would set that up in the comment controller because the user id is really just another attribute of that comment.

Additionally, I find it best to abstract this logic to a repository in the case you will need to eventually create a comment from another controller or somewhere else in your app. Maybe if the user takes some action you want to auto-generate comments when those actions are taken. The repository could look like this...

class CommentRepository {

    protected $comment;

    public function __construct(Comment $comment)
    {
        $this->comment = $comment;
    }

    public function newComment($user_id, $content)
    {
        $comment = $this->comment->newInstance();
        $comment->user_id = $user_id;
        $comment->content = $content;
        $comment->save();
        return $comment;
    }
}

Then you'd inject that repository into your controller which would look something like this...

class CommentController extends BaseController {

    protected $cr;

    public function __construct(CommentRepository $cr)
    {
        $this->cr = $cr;
    }

    public function create()
    {
        $comment = $this->cr->newComment(Auth::user()->id, Input::get('content'));

        return Redirect::route('comments.index');
    }
}

There are a few benefits to this approach. One as I said earlier, it makes your code reusable and easy to understand. All you need to do is inject the repository into your controller where you need it. Two is it becomes much more testable.

Upvotes: 0

Arthur Samarcos
Arthur Samarcos

Reputation: 3299

You can always get the authenticated user info using these methods:

//Will return the authenticated User object via Guard Facade.
$user = \Auth::user(); 

//Will return the User Object that generated the resquest via Request facade.
$user = \Request::user();

If you set your route to something like this:

Route::get('posts/{posts}/comments/create', 'CommentsController@create');

Then you can create a button (i'll use bootstrap here and hipotetical ids) that points to:

<a href="posts/9/comments/create" class="btn btn-primary">Create</a>

On your CommentsController you can have something like this:

public function create($post_id)
{
   $user = .... (use one of the methods above);
   $post = ... (get the post to be commented, if thats the case)
   ... Call the create comment function
   return redirect(url('posts/9'));
}

Upvotes: 1

pjobs
pjobs

Reputation: 1247

Immediate answer would be CommentController , this is the controller that should add/delete/edit comments.

Can any one else add/delete/edit comments other than users? If yes, are they going to go into same business/domain object?

Lets say if you have User Comments and Customer Comments have separate Business/Domain comment objects , in this case you may have separate UserCommentsController and CustomerCommentsController.

And as @Arthur Samarcos suggested you can get user info.

Upvotes: 0

Related Questions