PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Thu Nov 21, 2019 8:17 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Sun Aug 08, 2010 3:33 pm 
Offline
Forum Newbie

Joined: Mon Aug 02, 2010 3:50 pm
Posts: 9
Hello, this is my first post on devnetwork. I'm looking forward to participating.

I am looking for critique (obviously) of a design I have come up with for a PHP-based website. Specifically, this is a content management system.

The idea is that the client registers legal Controller class names with an instance of the Router class (seen in index.php below). The Router instance retrieves the currently specified Controller class name via GET or POST and then determines if it is a legal name; if it is legal, then the controller is instantiated, if it is not legal, then the page is redirected and a default Controller is display (such as a MainMenu class for example).

I am utilizing the Model-View-Controller design for the controller classes, but am specifically interested in feedback about my Router class. I don't have a peer-group of programmers to fall on and am looking forward to some constructive criticism! Thanks!

Here are two UML diagrams for my setup:
Router:
Controllers:

index.php:
Syntax: [ Download ] [ Hide ]
//Instantiate Router instance
$router = new Router();

//Register legal Controller class names and their include URLs with Router instance
$router->register('GeneralError', 'include/dir/GeneralError.php');
$router->register('MainMenu', 'include/dir/MainMenu.php');
$router->register('GalleryManager', 'include/dir/GalleryManager.php');

//Initiate the Router instance
$router->route();
 



`ARouter` class:
Syntax: [ Download ] [ Hide ]
abstract class ARouter
{
        private $legalClassNames = array();
       
        abstract public function route();
        abstract protected function defaultRoute();
       
        public function register($className, $includeUrl)
        {
                $this->legalClassNames[] = array('className' => $className, 'includeUrl' => $includeUrl);
        }
       
        final protected function isLegal($className)
        {
                foreach($this->legalClassNames as $array)
                {
                        if(in_array($className, $array)) {
                                return true;
                        }
                }
               
                return false;
        }
       
        final protected function includeByClassName($className)
        {
                $includeUrl = $this->getIncludeUrlByClassName($className);
                include $includeUrl;
        }
       
        private function getIncludeUrlByClassName($className)
        {
                foreach($this->legalClassNames as $array)
                {
                        if(in_array($className, $array)) {
                                return $array['includeUrl'];
                        }
                }
                return NULL;
        }
}
 


`Router` class:
Syntax: [ Download ] [ Hide ]
class Router extends ARouter
{
        protected function defaultRoute()
        {
                header('Location: index.php?page_name=MainMenu');
        }
       
        public function route()
        {
                if(($_SERVER['REQUEST_METHOD'] == 'POST') || ($_SERVER['REQUEST_METHOD'] == 'GET'))
                {      
                        //Request type is GET or POST
                        $this->getPageName();
                }
                else
                {
                        //Request type is not GET or POST
                        $this->defaultRoute();
                }      
        }
       
        protected function getPageName()
        {
                if(!isset($_REQUEST['page_name'])) {
                        $this->defaultRoute();
                } else if(empty($_REQUEST['page_name'])) {
                        $this->defaultRoute();
                } else if(!$this->isLegal($_REQUEST['page_name'])) {
                        $this->defaultRoute();
                } else {
                        //Page name is a legal class name
                        $this->initPage($_REQUEST['page_name']);
                }
        }
       
        private function initPage($className)
        {
                $this->includeByClassName($className);
                $this->page = new $className();
                $this->page->init();
        }
}
 


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group