How to follow Laravel's controller action naming convention when multiple methods fall under the same action?

Viewed 185

I am developing an app in Laravel for the first time. After watching a few videos, I discovered I missed the "follow the action naming convention" rule. (Incidentally, is there a name for that convention? REST doesn't seem to quite match, but maybe I just don't know REST well enough)

My current setup has a controller SupportTicketController, which includes the methods open() and close() to open or close the support ticket. If I understand correctly, these are update actions. I also already have an update() action to change the details on the ticket (which requires a different user role from opening/closing).

What is the appropriate way to convert multiples of the same action into Laravel's naming convention?

1 Answers

REST in my opinion is not about creating multiple actions, but creating single actions, that act within a specification. In your case that a PUT / PATCH call, will change a support ticket. So when new people exploring your API and knows REST, they how to to modify a support ticket.

Instead of going with multiple actions. The right aproach would be to do all actions under update and base it on the model. Imagine your support ticket in JSON like so.

{
    "id": 1,
    "status": "open",
    "message": "..."
}

Instead of thinking i have to make an open action, accept the status as a parameter on the update call. So if you want to update your support ticket to open and closed, do it like so. Then you only need to have one update action and your API from the outside will be slimmer and easier to understand.

url: api/tickets/1
action: PATCH
body:
{
    "status": "closed",
}

Sometimes there is a need to do single actions, i often do it if you have to start a job or send an email. But if you went with your original design i think the actions called open and closed is fine. Laravel does not have a naming convention outside of resources and there it is fairly free.

Related