Вопрос

** 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.

Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top