views:

62

answers:

1

Okay, so I am creating an MVC framework in PHP and I want it to be pretty flexible. This is all fine - I'm working on this at the moment and things are going well (So I don't want to use Zend or another pre-existing framework, haha!), but I wanted to make both the framework and application quite dynamic. I'll try explain:

Here's a simplified directory structure:

- index.php           (wants to use app1 or app2 depending on domain name)
- /app1               (wants to use framework 1.1)
    - /config
        - config.php  
- /app2               (wants to use framework 1.2)
    - /config
        - config.php  
- /framework_1.1
- /framework_1.2

A Bootstrap file /index.php receives all incoming requests. It will then load a certain application's config file from /app1/config/config.php or /app2/config/config.php, etc, depending on a certain condition, say.. the HTTP host:

// /index.php
switch ( $_SERVER[ 'HTTP_HOST' ] ) {
    case 'localhost':
        $app_root = ROOT . 'app1/';
        break;
    case 'site.com':
        $app_root = ROOT . 'app2/';
        break;
    default:
        $app_root = ROOT . 'application/';
        break;
}
define('APP_ROOT', $app_root);

The bootstrap file then loads the application's config file:

// /index.php
include( APP_ROOT . 'config/config.php' );

A $config array will be returned from the application's config file, which will indicate where the framework files are located.

// /app2/config/config.php
$config['framework_root'] = '/framework_1.2/';

Bootstrap runs that framework.

// /index.php
include( $config['framework_root'] . 'config/bootstrap.php' );

Is this the best way to go about this?

The only problem with this, is that the /index.php file has to know about all possible applications, so the user will need to edit the switch statement in /index.php (or a /apps.php file that the /index.php includes, maybe?).

Also, the application's configuration file has to be loaded before loading the framework's files, which seems a bit weird to me...

Is there a simple way for making it so the request specifies application, and the application specifies which framework to use, or is the above way the simplest?

Sorry if this was confusing! Confused me a bit writing it ;)

A: 

I use Symfony most of the time, so I will apply my knowledge there when answering your questions:

Is there a simple way for making it so the request specifies application

Use multiple "index.php" (or use ?app=xxx) with a URL-rewrite combo. That makes things a bit cleaner.

Also, the application's configuration file has to be loaded before loading the framework's files, which seems a bit weird to me...

It's not weird, you certainly need to configure and initiate some states before bootstrapping your framework, but perhaps you may want to provide sub-class of your framework's bootstrap class, to define what shall be done during framework bootstrapping.

I recommend you to look into Symfony2 to find out how they modularize/manage libraries and dependencies. Seriously, there's no reason to write another framework but to contribute to an existing one - unless you find them not sharing the same visions with yours.

yclian
I've taken a look at Symphony but I don't think I know enough about namespaces to be able to follow the code2 effectively. I will continue to study it though. The problem with multiple "index.php" files is the fact that a lot of code would be duplicated over and over, and any small change would need to be replicated throughout every file. Sorry, I don't quite understand what you mean by the sub-classing of my bootstrap class to define what should be done during the bootstrapping.. Could you elaborate? Thanks for the advice! And I don't think I'm quite smart enough to contribute just yet ;)
manbeardpig
Take a look at ProjectConfiguration of Symfony. Every new project will have a bootstrap file extending it.Code repeat can be eliminated with helpers and object-oriented inheritance :)
yclian
Okay, but let's say my AppConfiguration class has to extend Configuration. If I'm calling my application config file before running my framework, how will the AppConfiguration know what Configuration is? Wouldn't Configuration be a class in the framework's /classes/ dir? Meaning it would change depending on which framework the application wants to run...?
manbeardpig
Think out of the box. Your ApplicationConfiguration/ProjectConfiguration/*Configuration can be either a standalone lib or a small part of your framework that will be first loaded before "starting your framework" (btw, you are writing a PHP app, not Java).Put behind that idea, it's blocking your creativity.
yclian
Haha okay =P I think I figured out my own sort of solution to this so I think it'll be okay now. What did you mean by "you are writing a PHP app, not Java"? I know they're different in a lot of ways but what exactly in my comment was that referring to?
manbeardpig
I actually was trying to say that you do not have to boot up a framework like how *usually* a Java application does.As you mentioned "starting the framework" a few times.
yclian