Page 1 of 1

Need Feedback: PHP Framework

Posted: Sun Feb 17, 2008 9:03 pm
by aliasxneo
So I'm creating my own PHP framework and am seeking some feedback. The basic pattern the framework will rely on is MVC. I've been fiddling with new ideas on how to implement and this is what I've come up with.

Sticking to the MVC pattern currently I am using a mix of controllers and pathing to navigate the website. Here are my current files:

phpcell/.htaccess

Code: Select all

RewriteEngine on
RewriteRule ^([A-Za-z].*)/ index.php?query=$1 [nc]
phpcell/index.php

Code: Select all

<?php
 
$system_path = "/home/joshg/public_html/phpcell/";
$query = $_GET['query'] . '/';
 
$queryParts = explode("/", $query);
array_pop($queryParts); // The last / is junk //
 
$controller = array_shift($queryParts);
$controllerPath = $system_path . "controllers/" . $controller . "/" . $controller . ".php";
 
if (!is_file($controllerPath))
{
    die("<b>Invalid URL</b>");
}
 
require_once($controllerPath);
 
if (!class_exists($controller))
{
    die("<b>Invalid controller file for {$controller}</b>");
}
 
$controllerClass = new $controller();
 
if (!sizeof($queryParts))
{
    if (!method_exists($controllerClass, "auto"))
    {
        die("<b>Invalid URL!</b>");
    } else {
        $controllerClass->auto();
    }
} else {
    $methodName = array_shift($queryParts);
    
    if (!method_exists($controllerClass, $methodName))
    {
        die("<b>Invalid URL!!</b>");
    } else {
        if (sizeof($queryParts))
        {
            $controllerClass->query = implode('/', $queryParts);
        }
        
        $controllerClass->$methodName();
    }
}
 
?>
phpcell/controllers/test/test.php

Code: Select all

<?php
 
class test
{
    var $query = "";
    
    function auto()
    {
        $this->echotest();
    }
    
    function echotest()
    {
        echo "test";
    }
}
 
?>
When going to the following url:

http://www.mysite.com/phpcell/test/echotest/

The word "test" is rendered on the page. Let me break down the process:
  • The url '/test/echotest/' is rewrote to 'index.php?query=test/echotest'
  • In index.php it starts by parsing off the first section which is 'test'
  • This first section has to be a controller. To check if it is it checks to make sure that the controller file exists (controllers/controller_name/controller_name.php). This is how all controllers will be organized.
  • If the controller exists then it tries to make a new instance of the controller. Every controller file will have a class with the same name as the controller.
  • Once it has the controller object it then proceeds to parse out the second part, which is 'echotest'.
  • If there wouldn't have been a second part then by default the framework will try to call a default method named 'auto()'. If the controller does not have a default method then it errors out, otherwise it calls it.
  • If it does get the second part than it makes sure that the class has a method named that second part. If not it errors out. The second part has to be a function of the first controller. Before it moves on it makes sure to check if there are anymore parts of the query, and if there is it passes the rest off to that first controller (where it can do whatever with it).
I hope that was clear. It's simplistic but complex at the same time. I hope to extend this functionality even more by supporting nested controllers. Basically one of the functions in the first controller would redirect to another controller thus creating a nested structure.

What I'm looking for is some feedback on this style. Would you use it? Do you see any immediate flaws? And if you have any suggestions that would also be helpful. Thanks in advance.

Cheers,
Josh

Re: Need Feedback: PHP Framework

Posted: Sun Feb 17, 2008 10:01 pm
by Christopher
I looks like a really good start. The two areas I would want to see some improvement are first implementing a programmable Router, and second to make the error dispatching more configurable that just die().

Re: Need Feedback: PHP Framework

Posted: Sun Feb 17, 2008 10:16 pm
by aliasxneo
arborint wrote:I looks like a really good start. The two areas I would want to see some improvement are first implementing a programmable Router, and second to make the error dispatching more configurable that just die().
Thanks for the input.

In reality I actually have a lot of this planned out. There's basically going to be a master class which represents the framework (Most likely going to call it Cell). I'm still debating on how controllers will gain access to this class but as of right now I'm thinking implementing a singleton so that all the controller share the same Cell. So it would look like:

Code: Select all

class controller
{
    var $cell;
 
    function __construct()
    {
        $this->cell = Life::getCell();
    }
}
I plan on making an object pool (or protein pool, catch the drift?) that is interfaced through the main Cell class. So it would be like:

Code: Select all

$person = $this->cell->proteins->get("Person", $arguments);
And as for your suggestion of error handling the Cell class would be a great candidate:

Code: Select all

$this->cell->appendError("Some error");
Or if it's fatal:

Code: Select all

$this->cell->fatalError("Some error"); // I plan on using Smarty so this could just render an error template then die() //
I managed to finish the skeleton of the framework.

http://youthapologetics.net/phpcell.zip

In case anyone else is interested.