A37583acaede070320d6f28146074934

Hi, this is my first time posting here. I am hoping for a critique of my code.

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 displayed (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 is the UML diagram for the Router class:
Router: http://www.miketmoore.com/shared/website_manager_v_03/PageRouter_01.jpg

<?php
//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();
?>

<?php
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;
        }
}
?>

<?php
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();
        }
}
?>

Refactorings

No refactoring yet !

Fd4770af46c031b76dd4693e9d539dcd

quantumSoup

August 9, 2010, August 09, 2010 23:32, permalink

2 ratings. Login to rate!

Just after a quick skim through the code, I'd rewrite getPageName() as follows:

protected function getPageName()
        {      
                // empty() will handle undefined variables just fine
                if(empty($_REQUEST['page_name'])) {
                        $this->defaultRoute();
                } else if(!$this->isLegal($_REQUEST['page_name'])) {
                        // it would be nice if users were informed that they
                        // are trying to access an invalid page
                        $this->notLegal($_REQUEST['page_name']);
                } else {
                        //Page name is a legal class name
                        $this->initPage($_REQUEST['page_name']);
                }
        }
23463b99b62a72f26ed677cc556c44e8

smartbloke

August 19, 2010, August 19, 2010 11:58, permalink

No rating. Login to rate!

Stripped some of the unnecessary bulk out of the abstract class.

<?php
abstract class ARouter
{
        private $legalClasses = array();
       
        abstract public function route();
        abstract protected function defaultRoute();
       
        public function register($className, $includeUrl)
        {
                $this->legalClasses[ $className ] = $includeUrl ;
        }
       
        final protected function isLegal($className)
        {
                return isset( $this->legalClasses[ $className ] ) ;
        }
       
        final protected function includeByClassName($className)
        {
                if ( $this->isLegal( $className ) )
                        include $this->legalClasses[ $className ] ;
        }
}
?>
D41d8cd98f00b204e9800998ecf8427e

aaaaa

January 29, 2011, January 29, 2011 14:17, permalink

No rating. Login to rate!
<?php
//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();
?>

<?php
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;
        }
}
?>

<?php
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();
        }
}
?>

Your refactoring





Format Copy from initial code

or Cancel