Jump to content

one script to server all architecture, good or bad?


wincen

Recommended Posts

I was wondering if using one script that servers all is a good architecture for a php application?  If it is used in a site which grows to have lots of traffic will it still hold up?  If not, what are some other suggestions?

 

this code i think is basically a front controller that calls other controllers depending on what the user sends to the server.  $driver is basically the controller for whatever page is to be called.

 

file: index.php

<?php

if (!in_array ($_GET['page'], array('index', 'contact', 'about')))
{
    $page = 'index';
} else {
    $page = $_GET['page'];
}

$driver = false;

switch ($page)
{
    case 'contact':
        include 'classes\contact.php';
        $driver = new Contact();
        break;
    case 'about':
        include 'classes\about.php';
        $driver = new About()
        break;
    case 'index':
    default:
        include 'classes\main.php';
        $driver = new Mainpage();
        break;
}

if ($driver)
{
    $driver->Display();
} else {
    die ('some type of 404 message');
}

?>

 

 

suggestions, improvements, criticisms are all welcome.  i'm trying to build my first OO php websites and would like to know if i'm on the right track.  plus i haven't used php in a few years so i might be out of touch with new features and developments.

 

i have read a few articles that state having a front controller, or one script to server all is bad because that's what apache already does... so this may be bad for performance.

 

 

 

Link to comment
Share on other sites

Using a front controller to set some universal data and then call a page controller to handle the page specific dealings is a good way to handle things.  I personally think that controllers are not good objects in the sense that they are tightly coupled with the view/model and hard to test.  That said they're a good place to start to expose yourself to some OO challenges and as a way to reuse some common behavior.

 

Looking at you code I can tell you right away that you'll want to make some changes.  Really, the worst of it is the switch statement.  You should be able to load a page dynamically through reflection.  If you're using PHP 5 and haven't used reflection before it's worth checking out.  I can also provide you with a facade I've written for the reflection API which allows you to create a new instance of a class and invoke a method.  The reason you want to lose the switch is because you'll end up having to add a new case for every page, which is a pain.  Instead you should use reflection and have something that looks roughly like so:

 

(note this is pseudo code)

$page = $_GET["page"];
$action = $_GET["action"];

try {
    $control = Reflection::getInstance($page, array($args));
    Reflection::invoke($control, $action, array($args));
} catch (Exception $e) {
    $error->handle($e);
}

 

Link to comment
Share on other sites

i have read a few articles that state having a front controller, or one script to server all is bad because that's what apache already does... so this may be bad for performance.

 

Maybe, if you have an incredibly busy site on a single server, you will run into file system concurrency factors negatively influencing performance. I think you'll be forced on multiple servers before that happens though.

 

I would like to read the articles you're talking about though.

Link to comment
Share on other sites

You should be able to load a page dynamically through reflection.

 

Even without reflection you can dynamically instantiate and execute a method of a certain class pretty simply without any need for a switch.

 

<?php

  if (isset($_GET['page'])) {
    if (file_exists("classes/{$_GET['page']}.class.php")) {
      include_once "classes/{$_GET['page']}.class.php";
      $obj = new $_GET['page']();
      if (isset($_GET['action']) && method_exists($_GET['page'],$_GET['action'])) {
        $obj->$_GET['action']();
      }
    }
  }

?>

Link to comment
Share on other sites

wow, thorpe and buyocat, those are some very nice solutions.  sweet.

 

i don't remember every article that stated using a front controller in php is bad, but one of them was the on posted by buyocat. 

 

There was also Ramus Lerdorf no-framework PHP MVC framework http://toys.lerdorf.com/archives/38-The-no-framework-PHP-MVC-framework.html in which he states

 

Just make sure you avoid the temptation of creating a single monolithic controller. A web application by its very nature is a series of small discrete requests. If you send all of your requests through a single controller on a single machine you have just defeated this very important architecture. Discreteness gives you scalability and modularity. You can break large problems up into a series of very small and modular solutions and you can deploy these across as many servers as you like

 

 

He brings this point up again in one of his replys to user comments:

I think you guys are missing the point. It isn't about OOP vs. Procedural. I could just as easily have written the controller code as a series of objects. The important point is not the style of the code, the important point is that you don't have a single monolithic controller that becomes your bottleneck and forces all your controller logic into one place because it turns what is supposed to be a discrete and modular architecture into a monolithic one.

 

I've been taught that implementing design patterns like front controller, MVC, and factory are very good practices when creating websites; but when the creator of php is saying that the front controller is a bottleneck it makes me question what i've been taught.

 

Link to comment
Share on other sites

This is one of those things you must play by ear.

 

I don't think most sites receive enough traffic for a single controller to become a bottleneck.

 

Also, there are some steps you must take every time someone visits a site in the page so they tend to be in a centralized location.

 

Keep in mind that a front controller should be short and to the point, don't allow any unnecessary processing in a site's entry point.

 

Also, you should only direct traffic that needs to go through the controller and let the web server handle everything else.  For example, there's no point to send a site's images through a controller.

 

Hope that helps some.

Link to comment
Share on other sites

I don't think most sites receive enough traffic for a single controller to become a bottleneck.

 

I agree. At the point that it does, you'll need multiple application servers, because you will at the very least be close to running out of computing power. When you use distributed computing, a gateway can decide which requests goes to which server, each with their own single point of entry.

 

An interesting read on this subject is PoEEA's "Distribution Strategies" chapter.

Link to comment
Share on other sites

Patterns of Enterprise Application Architecture -- by Martin Fowler.

 

It's the second most influential Design Pattern catalog, but the narrative chapters also provide some interesting thoughts on concurrency issues and distributed computing.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.