Skip to content
This repository has been archived by the owner on Sep 13, 2018. It is now read-only.

Commit

Permalink
Completed a part of dispatch and router
Browse files Browse the repository at this point in the history
  • Loading branch information
pksunkara committed May 11, 2010
1 parent 7171bf5 commit 37018af
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/configs/routes.php
Expand Up @@ -18,6 +18,6 @@
* Here, we are connecting '/' (base path) to controller called 'Pages' and action 'home'
* (in this case, /app/views/pages/home.ctp)...
*/
Router::connect('/', array('controller' => 'pages', 'action' => 'home'));
Router::connect('/', array('controller' => 'ps', 'action' => 'home'));

?>
8 changes: 1 addition & 7 deletions app/www/index.php
Expand Up @@ -14,12 +14,6 @@
* @license GPLv3
*/

// Enable output buffering
ob_start();



// Flush output buffer
ob_flush();
$dispatch = new Dispatcher(URL);

?>
12 changes: 12 additions & 0 deletions sun/boot.php
Expand Up @@ -53,6 +53,12 @@
require_once CORE.'captcha.php';
}

/**
* Basic class for Controllers
* Usage: "Controller::"
*/
require_once CORE.'controller.php';

/**
* Basic class for parsing and dispatching URLs
* Usage: "$Dispatcher->"
Expand Down Expand Up @@ -131,6 +137,12 @@
require_once CORE.'rss.php';
}

/**
* Basic class for routing actions and pages
* Usage: "Router::"
*/
require_once CORE.'router.php';

/**
* Basic class for sanitizing strings and conversion
* Usage: "Sanitize::"
Expand Down
28 changes: 28 additions & 0 deletions sun/controller.php
@@ -0,0 +1,28 @@
<?php
/**
* sunFW(tm) : PHP Web Development Framework (http://www.suncoding.com)
* Copyright 2010, Sun Web Dev, Inc.
*
* Licensed under The GPLv3 License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2010, Sun Web Dev, Inc. (http://www.suncoding.com)
* @version 1.0.0
* @modifiedby Pavan Kumar Sunkara
* @lastmodified Apr 10, 2010
* @license GPLv3
*/

class Controller extends Object {

/**
* Constructor.
*/
function __construct($name) {

}

}

?>
22 changes: 21 additions & 1 deletion sun/dispatcher.php
Expand Up @@ -51,7 +51,27 @@ function __construct($url = null) {
* @return boolean Success
*/
function dispatch() {
$filename = $this->params['controller'].'.php';
$classname = Inflector::camelize($this->params['controller'].'_controller');

if(file_exists(CONTROLLERS.$filename))
require_once CONTROLLERS.$filename;
else {
$this->params = Router::getLink($this->params);

$filename = $this->params['controller'].'.php';
$classname = Inflector::camelize($this->params['controller'].'_controller');

if(file_exists(CONTROLLERS.$filename))
require_once CONTROLLERS.$filename;
}

if(!class_exists($classname)) {
print "Missing $classname";
}
else {
print $classname;
}
}

/**
Expand Down Expand Up @@ -90,7 +110,7 @@ function extract() {
break;
}
}

return $params;
}

Expand Down
1 change: 0 additions & 1 deletion sun/inflector.php
Expand Up @@ -497,7 +497,6 @@ function slug($string, $replacement = '_') {
);
return preg_replace(array_keys($map), array_values($map), $string);
}
}

/**
* Enclose a string for preg matching.
Expand Down
2 changes: 1 addition & 1 deletion sun/paths.php
Expand Up @@ -14,7 +14,7 @@
* @license GPLv3
*/

define('CONFIGS', APP.'config'.DS);
define('CONFIGS', APP.'configs'.DS);

define('CONTROLLERS', APP.'controllers'.DS);

Expand Down
98 changes: 98 additions & 0 deletions sun/router.php
@@ -0,0 +1,98 @@
<?php
/**
* sunFW(tm) : PHP Web Development Framework (http://www.suncoding.com)
* Copyright 2010, Sun Web Dev, Inc.
*
* Licensed under The GPLv3 License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2010, Sun Web Dev, Inc. (http://www.suncoding.com)
* @version 1.0.0
* @modifiedby Pavan Kumar Sunkara
* @lastmodified Apr 10, 2010
* @license GPLv3
*/

class Router extends Object {

/**
* Route mapping variable
*/
protected static $map = array();

/**
* Constructor.
*/
function __construct() {

}

/**
* Connector which connects one URL to another params.
* @param mixed $url relative URL, like "/products/edit/92" or "/presidents/elect/4"
*/
function connect($url,$params) {
$paramsnew = self::extract($url);
array_push(self::$map,array($paramsnew,$params));
}

/**
* GetLink which examines the params it recieve and do proper inclusion if connected
* @param mixed $params
*/
function getLink($params) {
for($i=0;!empty(self::$map[$i]);$i++) {
if($params['controller'] == self::$map[$i][0]['controller']) {
if($params['action'] == self::$map[$i][0]['action']) {
$params['controller'] = self::$map[$i][1]['controller'];
$params['action'] = self::$map[$i][1]['action'];

return $params;
}
}
}
}

/**
* Extracting controller, action and parameters from the URL
* @param mixed $url relative URL, like "/products/edit/92" or "/presidents/elect/4"
* @return array $params Array with keys 'controller', 'action', 'params'
*/
function extract($url) {
$params = array();
$url = explode('/',$url);

if (empty($url[0])) {
array_shift($url);
}

if (empty($url[0])) {
$params['controller'] = 'pages';
} else {
$params['controller'] = $url[0];
}

array_shift($url);

if (empty($url[0])) {
$params['action'] = 'index';
} else {
$params['action'] = $url[0];
}

array_shift($url);

foreach ($url as $key=>$val) {
if (!empty($val)) {
$params['params'][$key] = $val;
} else {
break;
}
}
return $params;
}

}

?>

0 comments on commit 37018af

Please sign in to comment.