Jeff PHP framework  0.99
Modular, extensible, OOP, MVC, lightweight php framework designed to ease the programmers in the development of web applications.
router.class.php
Go to the documentation of this file.
00001 <?php
00025 class router {
00026 
00030         private $_registry;
00031         
00035         private $_base_path;
00036         
00040         private $_module;
00041         
00045         private $_method;
00046 
00053         function __construct($base_path) {
00054 
00055                 $this->_registry = registry::instance();
00056                 $this->_base_path = $base_path;
00057 
00058         }
00059 
00065         public function module() {
00066 
00067                 return $this->_module;
00068 
00069         }
00070         
00076         public function method() {
00077 
00078                 return $this->_method;
00079 
00080         }
00081 
00093         public function loader($route) {
00094 
00095                 /*** check the route ***/
00096                 $this->getModule($route);
00097 
00098                 /*** if the file is not there diaf ***/
00099                 if(!is_readable(ABS_MDL.DS.$this->_module.DS.$this->_module.'.controller.php'))
00100                 {
00101                         Error::syserrorMessage('router', 'laoder', sprintf(__("CantChargeModuleControllerError"), $this->_module, ABS_MDL.DS.$this->_module.DS.$this->_module.'.controller.php'), __LINE__);
00102                 }
00103 
00104                 /*** include the controller ***/
00105                 include_once(ABS_MDL.DS.$this->_module.DS.$this->_module.'.controller.php');
00106 
00107                 if($route == null) $this->_registry->urlModule = $this->_module;
00108 
00109                 /*** a new controller class instance ***/
00110                 $class = $this->_module."Controller";
00111                 $controller = new $class();
00112 
00113                 /*** check if the method is callable ***/
00114                 $method = is_callable(array($controller, $this->_method)) ? $this->_method : 'index';
00115 
00116                 $params = gOpt($route, 'params');
00117                 /*** run the action ***/
00118 
00119                 return $controller->$method($params);
00120         }
00121 
00131         public function getModule($route) {
00132 
00133                 if(is_array($route) && isset($route['module']) && isset($route['method'])) {
00134                         $this->_module = $route['module'];
00135                         $this->_method = $route['method'];
00136                 }
00137                 else { /*** get the route from the url ***/
00138                         $this->_module = isset($_GET['module']) ? $_GET['module'] : 'index';
00139                         $this->_method = isset($_GET['method']) ? $_GET['method'] : 'index';
00140                 }
00141         }
00142 
00158         public function linkHref($module, $method, $params=array(), $opts=null) {
00159 
00160                 $permalink = gOpt($opts, 'permalink', true);
00161 
00162                 $href = '';
00163                 if($module) $href .= $permalink ? "/".$module : "&module=$module";
00164                 if($method) $href .= $permalink ? "/".$method : "&method=$method";
00165                 if(count($params)) {
00166                         if(isset($params['id']) && $params['id']) $href .= $permalink ? "/".$params['id'] : "&id=".$params['id'];
00167                         foreach($params as $k=>$v) {
00168                                 if($k!='id') $href .= $permalink ? "/".$k."/".$v : "&$k=$v";
00169                         }
00170                 }
00171                 $href .= $permalink ? "/" : "";
00172 
00173                 return $this->_base_path.($permalink ? $href : "/?".substr($href, 1));
00174         }
00175         
00187         public function linkAjax($module, $method, $params=array()) {
00188 
00189                 $href = $this->_base_path."/pointer.php?module=$module&method=$method";
00190                 if(count($params)) {
00191                         foreach($params as $k=>$v) {
00192                                 $href .= "&".$k."=".$v;
00193                         }
00194                 }
00195 
00196                 return $href;
00197         }
00198 
00199 }
00200 
00201 ?>