tags:

views:

108

answers:

3

Hello all!

Ok, it's my fault. I've never ever learned programming at a school and that's why i'm always ending up in a spaghetti code. I've always curious about different patterns and tried to understand them at least in a basic level.

MVC is my worst fear and i think i'll be never able to use it's advantages because of i don't understand it's fundamentals.

My actual question/problem looks like:

The front controller calls a 'Core' class which is doing some initialization then it calls the actual controller with the correct action/parameters. The controllers always extending the 'Core' class so i can acces it's variables, etc. They're working nicely together but here comes my real problem.

Some kind of methods (getting a database entry in most of the cases) are required in different cases. (e.g. a product needs it's manufacturer)

In this scenario i have two (bad) choices:

  • Inject the required method into the 'Core' class so it's getting bloated over time
  • Inject the required method into the actually called controller so i will end up a redundant codebase

I see a lot of possible problems in my approach:

  • Controllers are always extending 'Core' class
  • 'Core' controller holds the database object so without it i cannot access my Db
  • Database functions (e.g. getting a product) are in the controllers but i cannot access them because they're always calling 'Core' first (extending problem again)

Please tell me:

Where is the biggest problem in my approach and where can i correct it?

Note:

Please don't treat this as a general question, i think this is an answerable thing. If you need some clarification, please ask for it and i'll try to lighten up things.

Thanks for your precious time, fabrik

+1  A: 

Your data is represented to your Controller and View through the Model. The Model may be supported by a Repository but depending on the overhead you might want to provide your database access in your Model. The data architecture should be similar to this:

(Repository<===>)Model<===>Controller--->View

Lazarus
Thank you for your answer. These are the basic principles of MVC and (as i said before) i'm not very familiar with it. This diagram is representative but what i'm looking for is how can i connect them effectively. Who calls who? Who holds what? What accountable for what? These are my biggest dilemmas. All of the tutorials i've read based on a framework which presumes you don't want to understand but use it.
fabrik
@fabrik: There's no clear answer, because not everybody agrees. There are a ton of implementations, and each does things slightly different. I'd suggest reading [Head First Design Patterns](http://oreilly.com/catalog/9780596007126)... It does a great job (IMHO) at explaining these things...
ircmaxell
@fabrik: In MVC I use Inversion of Control through Dependency Injection, so when my Controllers are instantiated they are populated with the Model object. The Controller methods can then retrieve data from the Model which in turn was instantiated with Repository objects (through DI). The Controller does not call the repository, it calls the Model, the Model calls the Repository. The Controller then instantiates the View and passes the View the data it needs (usually within a ViewModel but that's likely t overcomplication things). The Controller sits in the middle and initiates everything else.
Lazarus
@Lazarus: You said "not everybody agrees". That's it. I've read almost all (PHP) frameworks' documentation (looked the source too) and each of them implements MVC differently. I see that no golden rule however i read more and more it looks more and more complicated. Thanks for the tip about the book.
fabrik
@fabrik: Actually it was @ircmaxell who said that but it's correct. It's that old situation, put two developers in a room, get at least three opinions on any subject :) The Head First books are a great reference too, get the Design Patterns one (you can get it as an ebook!).
Lazarus
@fabrik The reason MVC implementations for PHP are so different might stem from the fact that PHP itself is poorly suited for MVC, and that any implementation will at best be a clever hack. ;]
bzlm
@bzlm I don't think that's it. PHP has never had a benevolent (or non) dictator telling us "what the proper implementation is". But realize something: there are tons of MVC implementations in all languages. MVC was crafted for stateful and reasonably long running applications. When we started pulling it into the Web environment, smart people started realizing that it didn't fit the mold exactly ("views" don't observe their models for changes, there's no need for that). So there's been little consensus in the broad sense over which tweak is best. Perhaps it says more about MVC than anything...
ircmaxell
@ircmaxell Well, surely you would agree that RoR has "hit the nail" re: MVC in a way no MVC framework for PHP has. Or was it just first?
bzlm
A: 

Your biggest problem is having the "Core" class, get rid of it asap. By the way, the FrontController is not the only way to do things MVC things either.

Your second problem is that controller deals with database, it shouldn't. I suggest you use some abstract data layer, which you use only in your models. And controller should deal only with models, it shouldn't care how models get their data persisted and fetched.

Anton N
How do you figure that's the biggest problem? There are plenty of uses for a common "core" class. Check out [Lithium Framework](http://lithify.me/), they use it for the vast majority of their classes...
ircmaxell
Most OO frameworks derive from a 'Object' class as the base of all classes. What's important to remember is why you are using inheritance and make sure that it's for the right reason. Controllers deriving from a Controller base class makes sense, data access class (i.e. Repository) deriving from a Controller class makes no sense.
Lazarus
People, Core class is ugly design. Period. What do controllers, data and utility objects have in common? That they are all objects? That doesn't make any sense. I agree though, that controllers might derive from some AbstractController class, but this inheritance shouldn't be used in the way it is described in the question (i.e. initialising and holding db connection)
Anton N
Oh, I'm not arguing that using inheritance to deal with anything concrete is a poor design at best. The point was made that there are some uses for it. For example, Lithium uses it to provide the common functionality of being able to chain callbacks around an object method. So by extending this "base" object class, you gain the ability to dynamically add functionality to classes at run time without needing to extend (basically compensating for the lack of mixin support in 5.3)...
ircmaxell
A: 

Look into using a DI framework to automatically inject an instance of a repository into your controllers (or even better, a proxy class). Try to keep business logic outside of your controllers, but rather, refector it out into a helper or proxy class.

I tend to split my logic up into views => controllers (just for interaction between business later and view) => business logic => models (DTOs) => low-level data access at a minimum.

Also, if you need common helper functionality in your views, perhaps create several extensions to help.

Ryan Peters