views:

64

answers:

3

I'm creating a PHP web application framework (MVC). I'd rather not use external libraries or components (as I want this to be purely my work for now)

Can you tell me some tips/guidance for what each of my files should be responsible for doing? Such as, what should be handled by the Framework script and what should be handled by an Application script used in the framework?

I keep changing where different code is written (as I think to myself... "Should this be handled by the framework or by each application?"), which is making it more confusing as I go along.

I've read a bunch (20...50...100 even!) of tutorials regarding MVC, frameworks etc, but there's not many that explain the ideal 'flow' of a framework..

Currently I have it working like this:

  • Main Index (index.php)
    • Defines constants (DS = DIRECTORY_SEPARATOR, PS = PATH_SEPARATOR, etc)
    • Sets include paths (ROOT . '/classes', ROOT . '/includes', etc)
    • Loads Framework configuration file (config.php)
    • Sets up framework class autoloader (__autoloader())
    • Sets up some sort of Core object (Core::init($config)?)
    • Loads Application index.php file (app/index.php)
  • Configuration (config.php)
    • Defines config array ($config)
    • Configures config array ($config['debug'] = 0... or something like that..)
  • Application Index (app/index.php)
    • Defines constants (APP_CONTROLLERS, APP_MODELS, etc)
    • Sets include paths (APP_PATH . '/classes', APP_PATH . '/includes', etc)
    • Loads Application configuration file (app/config.php)
  • Application Configuration (app/config.php)
    • Defines config array ($app_config)
    • Configures config array (Same sorta stuff as the other config, but for App)

Now.... do I seem to be heading in the right direction? And what sort of things should the Framework's Main Index script be doing rather than the App Index? Should the Main index just initiate some stuff and pass off most of the work to the App Index, which will set up routers etc to route URLs to controllers, etc...? Or should the Framework be creating routers and initiating controllers, and the App would just set controller paths and some rules, etc..?

I understand what Controllers do, and models/views etc (I'll cross those bridges when i come to them later on), but for now I just want to get the basic things flowing correctly from the right places so things later on will work well.

At the moment, my head's about to explode! haha

This may have even been a really dumb question, but I just think I need some straight-forward guidance to help me clear things up before I get everything totally out of whack! Any advice would be appreciated.

Thanks =)

+3  A: 

You should put together a few applications in a popular MVC framework or two before creating your own. That way you will have a better idea of how things should flow.

Remember you are reinventing the wheel... poorly. But it sure is fun eh ? ;)

Byron Whitlock
A very obvious but good answer haha. I have been studying different frameworks but havent' really taken the time to create applications within those frameworks. I'm having a look at CakePHP at the moment... and strangely enough, most of the code from that seems to be very similar to my code. Almost creepily similar haha! But yeah, I'm a big fan of reinventing the wheel. It's one of the most educational (and quite time-wasting =P) experiences you can do! And I've learned so much already from it.
manbeardpig
+2  A: 

There is a great tutorial on writing your own mvc framework, however as Byron said, you will be re-inventing the wheel. I think that it is worth having a go at writing your own first though as its a great learning experience and unlike a big mvc framework(symfony or zend) you will know what every part of the code does. This tutorial is the one I used to learn about mvc before moving onto using Symfony, it helped me alot with understanding how symfony works. After Anant Garg's tutorial I moved onto the Jobeet Symfony tutorial which really helps explain symfony 1.4.

Hope this helps

Luke

Luke
I think the first tutorial was one of the first I read on the subject, but I haven't really gone back to it lately.. which was soemthing I should have done. After reading a bunch of tutorials, things sort of got mixed up in my mind, so hopefully rereading that article will help. I may eventually move to a 'real' framework, but for now I'm pretty set on making my own. I will take a look at the second one after I reread the first. I am also looking at the actual 'cookbooks' and guides to various frameworks (Cake at the moment) so that may shed some light for me too. Thanks for your help!
manbeardpig
+1  A: 

One thing to remember is that MVC is just a pattern, and therefore is not meant to be 100% strict but rather a guidance for implementation that can be tweaked to fit your specific needs. So as others have suggested, try with what you have and you'll see what works for you and what doesn't.

SBUJOLD