-1
\$\begingroup\$

I'm using Kohana's routing with the route being defined as domain/controller/action. To demonstrate my question let's consider sign-in flow. Currently, if I want to sign-in user I need to return form first and then proceed with data sent by user. Do I need to have two actions, each for the corresponding flow (returning flow and proceeding with user data), or it can be done with one action by differentiating between POST and GET requests? I have one action now which returns form template if GET request is made and proceeds with signing in if POST request is made, but I question that architecture. Here is the sample of code:

public function action_signUp() {
    if (HTTP_Request::POST == $this->request->method()) {
        //proceed with user sent data
    } else if (HTTP_Request::GET == $this->request->method()) {
        //return form template
    }
}
\$\endgroup\$
  • \$\begingroup\$ Can you not route get and post requests to different actions? \$\endgroup\$ – Cameron Martin Mar 19 '14 at 11:53
  • \$\begingroup\$ Sure I can, I just want to know the best practice :) \$\endgroup\$ – Max Koretskyi aka Wizard Mar 19 '14 at 12:14
  • \$\begingroup\$ Is there any difference in the controller logic for each branch (except rendering different content)? It would be helpful to post the code inside the conditionals. \$\endgroup\$ – Cameron Martin Mar 19 '14 at 12:55
  • \$\begingroup\$ The difference is that GET branch simply renders log-in view, while the POST branch logs in user and returns redirect \$\endgroup\$ – Max Koretskyi aka Wizard Mar 19 '14 at 13:41
2
\$\begingroup\$

The single responsibility principle also extends to methods. By using one action, you are creating a method which does two entirely unrelated things, which violates this principle.

Also, you are making the controller concerned with which http verbs map to which action, which is responsibility of the router. By splitting the method up, you can later make different urls or verbs point to each action, without changing your controller. See separation of concerns

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.