-1

I am pretty much new into this world of the PHP MVC, so I apologize for any obvious answer.

When it comes to the Controller, should I create multiple individual files like "login.php" and "logout.php" and then have the login form action point to "controller/login.php"?

form action="controller/login.php" method="post"

or

form action="controller/controller.php" method="post"

In case the second is the correct one, should I include or extend the "login.php" (assuming it should still exist in a different file) into the controller.php?

I am just wondering if from a design perspective this is correct. I have read: Should I extend Controller or Create Helper? Single Controller with Multiple Views ,which seem to be similar (with a different language though), but I am still not sure that I got it.

Thanks in advance,

Community
  • 1
  • 1
Portu
  • 542
  • 2
  • 4
  • 16
  • Please, read about [SRP](http://en.wikipedia.org/wiki/Single_responsibility_principle) and think on how it apply to [SoC](https://en.wikipedia.org/wiki/Separation_of_concerns). – tereško Sep 04 '13 at 15:39
  • Great links, I'll keep them handy. Basically, since "login.php" and "logout" are two different actions, I should have then as two different controllers. Thanks Tereško :) – Portu Sep 04 '13 at 15:52
  • You misunderstood a bit. Controllers in general are responsible for altering state of model layer and (sometimes) current view instance based on user input. Actions like "login" and "logout" are associated with authentication-related aspects of user input and therefore should be part of same controller. – tereško Sep 04 '13 at 17:22
  • Just to clarify, from an MVC perspective, of course, when you say that they are associated to the same controller you mean something like this: controller --> authentication --> login.php & logout.php ? – Portu Sep 04 '13 at 22:48
  • No. I means something like `$controller = new Controllers\Authentication( $serviceFactory, $view ); $controller->postLogin( $request );` – tereško Sep 04 '13 at 22:51
  • Any source that you would recommend for reading about MVC, (from your personal library)? your concept and understanding of this model is out of my scope and I just want to make sure I understand the right way to do it. Out there are tons of models and designs, but it is very difficult to determine which one is right or not. – Portu Sep 05 '13 at 00:35
  • You could try [this list](http://stackoverflow.com/a/16356866/727208) – tereško Sep 05 '13 at 05:36

1 Answers1

-1

Controllers usually are not just a single action. Controllers represent a group of actions for a specific module. For instance, a user controller would have typical CRUD actions, and then maybe a login/logout action, and possibly a register(under create maybe) or forgot password action. If you are making a blog, a post/entry controller would most likely just have the typical CRUD actions and maybe a comment action. So basically one controller per module or per model even.

All your controllers should extend a similar base, but it doesn't have to be the same for the entire project.

Your forms would look like: action="controllerName/actionName" or action="controller.php?q=controllerName/actionName depending on how you have the mod-rewrite setup.

As far as views, each controller action could potentially have multiple views (depending on POST or GET data), but it is pretty standard for each action to have a single view.

Tim Withers
  • 12,072
  • 5
  • 43
  • 67
  • Got it, so I will keep them in two separate files and point to action="controllerName/actionName", in my case action="controllers/login.php". I do appreciate your time and collaboration Tim. – Portu Sep 04 '13 at 15:53
  • 1
    -1: controllers have nothing to do with CRUD, model is a layer not any single class or instance, there is not requirement for controllers to extend same superclass, views are not templates and finally - your recommended routing scheme is unsustainable. Basically, everything but the first two sentences in this answer is **WRONG**. – tereško Sep 04 '13 at 17:24
  • Controllers don't have anything to do with CRUD, they have everything to do with ACTIONS as I said in the second sentence. I was giving an example of ACTIONS associated with a controller. I never once mentioned templates... so I don't know where you got that. And I didn't even address routing, just gave an example on how one would specify a controller and an action. Try reading next time @tereško – Tim Withers Sep 04 '13 at 17:47