1

Earlier I asked this question: should-i-create-an-object-or-work-with-an-array

I am now trying tho think beyond the concept that I was working with. Please share your thoughts with me. I want to GET this.

If I would setup an MVC in combination with a data mapper, would this be logical, for a forum for instance:

All important things are entities. Post, Thread, User, Forum. Basically I see a controller as a page. It may choose to show different templates (e.g. list, or form), but it IS the page, more or less.

Via the router I would have the needed controller loaded, to get the data and have it displayed in my template.

Now how would this work, for displaying all posts in a thread:

route is set to thread-> load thread controller -> controller asks entities (post, user) for the info -> entities tell the mapper what they need -> mapper gets it from database and returns it to entities -> entities return info to controller -> controller returns info to view -> view displays.

Is that the right idea?

Now where did the "model" go then, from the MVC? Or am I missing steps?

I do not want to use third party tools, I want to build it from scratch, to understand everything that goes on.

How do I start this off right?

Community
  • 1
  • 1
LvS
  • 507
  • 1
  • 4
  • 19

1 Answers1

-1

I tend to see the page as the sum of the controller (the logic required prior to rendering) and the view (which determines what the page looks like). In general, I would recommend that you don't write your own first, since you'll be working to a design pattern that you've not yet fully understood. I think it would be better to pick a popular PHP framework and see how it is implemented, and then if you are still minded to, try writing your own. Coding it all of this is a lot of work, in my view, especially if you want to write an ORM too!

Although we shy away from framework recommendations on Stack Overflow, I understand from comments here that Symfony2 is thought to be one of the best implementations of this design pattern, in particular because of its use of dependency injection. Do read some of the questions here with the 'mvc' tag too - there's plenty that can be picked up. For what it's worth, I wouldn't get hung up too much on design patterns (i.e. whether framework X implements pattern Y) - so long as your apps are modular and easily testable, that's a big win.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • thank you for your recommendation. I actually started out with CodeIgniter and after getting annoyed by having to conform to their code, I decided I wanted to write my own framework. I have also worked with a couple of webshopsystems, like OpenCart, that have a good working MVC model going on. I read the system's code and try to get what they are doing and why. And I have followed some courses at Lynda.com, wich also pointed me in other directions. – LvS Jun 01 '13 at 14:53
  • For instance in the tutor was pretty enthousiastic about using statics, while the other was not a fan of it but promoted the Singleton enthousiasticly. And then on here, all those different opinions. I actually think youre quite right: I should just make an app that works, not get hung up. – LvS Jun 01 '13 at 14:54
  • It's worth knowing that there is a recent backlash against the Singleton pattern - personally I tend to avoid it because it hard-wires against class extensibility. However, it turns out [there's plenty of other reasons too](http://stackoverflow.com/q/1392315/472495)! – halfer Jun 01 '13 at 15:49
  • Controller must no contain application logic. Look up how the pattern is defined. Controllers only task are related to altering state of model layer and views. – tereško Jun 01 '13 at 17:35
  • @halfer Yes I have been reading a lot about the pros and cons of the Singleton after somebody calling it "evil" here. I am going to recode that part. And also the statics that I was using with the course in mind. Basically... ever since i started to really get into the code, I have bounced from one camp to the next... it's hard to read the real wrongs and rights trough all the opinions. I so badly want to get it RIGHT. – LvS Jun 01 '13 at 19:30
  • @tereško Thanks for your remark. Do you happen to have a good link where the MVC is explained in the way that you see it? Would like to read that. – LvS Jun 01 '13 at 19:31
  • @LvS - getting it right is fine, but getting it perfect doesn't often survive cost/benefit analysis (and 'analysis paralysis' can be the result of over-design). I think our newest interlocutor means that one should have a _service layer_, though most PHP frameworks roll that into the controller. In my experience, if that is your only "problem", you can still have a modular, flexible, testable application. – halfer Jun 01 '13 at 19:44
  • @LvS , most of materials, that I use, come from Fowler. You can find the significant ones [here](http://stackoverflow.com/a/16356866/727208) (the last section is). The rest is more along the lines of "how to apply oop and mvc concepts to php". – tereško Jun 02 '13 at 07:08
  • @halfer Suggesting the use of a framework doesn't really answer the question. Maybe a reference would've been good? I cant argue with your point though. – Zee Nov 15 '13 at 22:07