2

** simplified question **

I am learning oop patterns and I am looking to build my own simple mvc framework. I would like this to have a front controller but I am finding it difficult to find any credible information for implementing a front controller with MVC.

In particular I am confused about whether the front controller should initiate the entire triad or whether the front controller simply calls the controller and the other parts do the rest.

I have noticed classes like route, router and bootstrap and I am wondering what these particular classes do and whether they are dependent on the front controller itself.

Michael Ramirez
  • 237
  • 5
  • 21
  • Far as I am concerned the definitive source is [PoEAA](http://martinfowler.com/eaaCatalog/), good book to own. Looked at [other MVC frameworks](https://github.com/search?q=PHP+MVC+framework&type=Repositories&ref=searchresults) and how they do it? Also, this was a good read for me a few years back: http://survivethedeepend.com/zendframeworkbook/en/1.0 – ficuscr Nov 08 '13 at 20:50
  • 1
    This post has 9 different questions. – tereško Nov 08 '13 at 20:50
  • I think this link would be a good start: http://net.tutsplus.com/tutorials/other/mvc-for-noobs/ – Jorge Campos Nov 08 '13 at 20:50
  • The mvc diagram in that post claims that the model interacts back and forth with the controller -- I thought this was not supposed to happen. I am mainly confused about the integration with the front controller not so much the understanding of mvc. – Michael Ramirez Nov 08 '13 at 20:53
  • 1
    @JorgeCampos that's actually a really terrible article. – tereško Nov 08 '13 at 20:54
  • @tereško Should I post these all individually? – Michael Ramirez Nov 08 '13 at 20:54
  • you probably should start by searching already existing answers in this site – tereško Nov 08 '13 at 20:56
  • I have but I can't find anything explaining mvc with the front controller – Michael Ramirez Nov 08 '13 at 20:58
  • @tereško It is just a matter of opinion, I think it is great for the ones like the OP (based on his questions) because show the really basic stuff and with a practical example using and showing a MVC ready to use. – Jorge Campos Nov 08 '13 at 21:00
  • @JorgeCampos that article has nothing to do with front controllers. It just explains mvc, plus it's wrong! – Michael Ramirez Nov 08 '13 at 21:03
  • @JorgeCampos nothing that is describe in that article even remotely associate with MVC. It only perpetuates the retarded myth that "rails is mvc". The author of article does not even have a base understanding for *Separation of Concerns* principle. – tereško Nov 08 '13 at 21:03

1 Answers1

3

Actually that's not a question, you're just trying to get suggestions on how to proceed while building your own MVC framework. So I'll try to provide an answer / consideration as generic as your question.

1) "I'm learning OOP Patterns": patterns are as much powerful as dangerous in the wrong hands. What I'm trying to say is that you should start building your fw without trying to use every patterns you come across the net just because it is used or talked about by the big ones. You can refactor you code later providing each step an higher level of abstraction: this will naturally involve using the patterns you'll be reading about and a better understanding of them.

2) "confused about whether the front controller should initiate the entire triad": that's up to which level of coupling you're aiming to have in your mvc. You can have your Front Controller handling everything like:

  1. bootstrap: load config and instantiate database connection and so on
  2. request: get the needed data describing what was asked
  3. route: handle the request
  4. response: return what was asked

But what if the configuration is needed somewhere else? Maybe in a CLI running script? You'll be naturally detaching the bootstrap component from the router to use it anywhere else is needed. And the same is for the other components.

3) "classes like route, router and bootstrap". Imagine to have your big class handling everything. How will you be testing your methods? Will you manually call the script with different inputs? Will every testing method have to check for the input, the routing and the output at once? Providing an abstraction level upon every component involved in your Front Controller encapsulating it in a proper class/object/module, will give you far better testing capabilities.

I'm talking because I've been down that road before creating exactly what you're talking about: https://github.com/OverKiller/PHP-Router

But I had to face hard testing capabilities and deep coupling. I'll be rewriting it soon, abstracting the request, the route and the response component. But I had my experience and I'm proud of it!

You should do the same. What I'm trying to say is: do not try to build the next Ultimate SymZendCakeIgniter PHP Framework all at once. Take your time, take your time to read and take your time to learn. And for god sake: *even before reading anything about design patterns get a nice book about T-E-S-T-I-N-G

I hope I was useful.

Damiano Barbati
  • 3,356
  • 8
  • 39
  • 51