zachleigh
243
10
General

Only CRUD....always best?

Posted 2 years ago by zachleigh

I've been hearing a lot of talk lately about how you should strive to have controllers that contain only CRUD operations. If your controllers go beyond this, its time to make a new controller. While I generally agree with this, I'm finding it very hard to fit all my actions within these limitations. For example, I have an invitation system in an app I'm building. In order to view a resource, you must be invited to it by a user who currently belongs to the resource. An invite is a record on the invites table with the resource id, name, and the user ids of the inviter and the invitee. I put this logic into an InvitesController. Currently it looks like this:

    /**
     * Show invite form.
     *
     * @return view
     */
    public function create()
    {
    // Show form to invite user
    }

    /**
     * Save the invite for the user.
     *
     * @param SendInviteRequest $request
     *
     * @return view
     */
    public function store(SendInviteRequest $request)
    {
    // Create invite in database
    }

    /**
     * Decline an invitation by deleting it.
     *
     * @param Request $request
     *
     * @return view
     */
    public function destroy(Request $request)
    {
    // Delete the invite
    }

    /**
     * Accept an invitation.
     *
     * @param Request $request
     *
     * @return view
     */
    public function acceptInvite(Request $request)
    {
    // Add user to resource users, delete invite
    }

So I have create, store, destroy, and ...... acceptInvite. How would I name acceptInvite in a CRUD way? What it does is it adds the user the resource's resource-user pivot table and then deletes the invite record. So would it be destroy? But that method is taken... Update? No, it destroys the invite. Maybe move it onto the resource's controller? But then what would I name it? Update? No...resources already have an update method. Or should I create a controller for the pivot tables just to accommodate this one method?

In these cases, what does everyone usually do? Try to push your logic into the confines of a CRUD method, or just give up and name it what you want? Im leaning towards the giving up option...I dont want to spend all day trying to name stuff.

Please sign in or create an account to participate in this conversation.