Overview

Namespaces

  • Application
    • Controller
    • Model
      • DAO
        • Impl
      • Service
        • Impl
  • Define
    • Core
    • DBDrivers
    • Exceptions
    • IUtilities
    • Traits
    • Utilities
  • None

Classes

  • Application\Controller\ApplicationController
  • Application\Controller\ErrorController
  • Application\Controller\IndexController
  • Application\Model\DAO\ApplicationDAO
  • Application\Model\DAO\Impl\IndexDAO
  • Application\Model\Service\ApplicationService
  • Application\Model\Service\Impl\IndexService
  • Define\Core\BaseController
  • Define\Core\BaseDAO
  • Define\Core\BaseModel
  • Define\Core\BaseService
  • Define\Core\Framework
  • Define\Core\ObjectContainer
  • Define\Core\Router
  • Define\Core\View
  • Define\DBDrivers\DatabaseBean
  • Define\DBDrivers\DatabaseFactory
  • Define\DBDrivers\MONGODBDriver
  • Define\DBDrivers\MSSQLDriver
  • Define\DBDrivers\MYSQLDriver
  • Define\DBDrivers\MYSQLIDriver
  • Define\DBDrivers\ORACLEDriver
  • Define\DBDrivers\PDODriver
  • Define\DBDrivers\POSTGREDriver
  • Define\DBDrivers\SQLITEDriver
  • Define\IUtilities\PasswordHelper
  • Define\Utilities\ErrorExceptionHandler
  • Define\Utilities\Localization
  • Define\Utilities\Logger
  • Define\Utilities\Session
  • PHPMailer
  • SMTP

Interfaces

  • Define\Core\IBaseDAO
  • Define\Core\IBaseModel
  • Define\Core\IBaseService
  • Define\Core\IDefine
  • Define\Core\IRouter
  • Define\Core\IView
  • Define\DBDrivers\IDatabase
  • Define\Exceptions\IException
  • Define\Utilities\IUtilities

Traits

  • Define\Traits\SingletonTrait

Exceptions

  • Define\Exceptions\FileNotFoundException
  • Define\Exceptions\FrameworkException
  • Define\Exceptions\LoggerException
  • Define\Exceptions\NullPointerException
  • Define\Exceptions\RuntimeException
  • phpmailerException

Functions

  • __autoload
  • Overview
  • Namespace
  • Class
  1: <?php
  2: namespace Define\Core;
  3: use Define\Core\IDefine;
  4: use Define\Traits\SingletonTrait;
  5: use Define\Exceptions\FrameworkException;
  6: 
  7: if(!defined('DIRECT_ACCESS')) {
  8:     die("Direct access is forbidden.");
  9: }
 10: 
 11: /**
 12:  * ROUTER
 13:  *
 14:  * Framework Router
 15:  *
 16:  * @category Define
 17:  * @package Core
 18:  * @author Nitesh Apte <me@niteshapte.com>
 19:  * @copyright 2015 Nitesh Apte
 20:  * @version 1.0.0
 21:  * @since 1.0.0
 22:  * @license https://www.gnu.org/licenses/gpl.txt GNU General Public License v3
 23:  *
 24:  * This program is free software: you can redistribute it and/or modify
 25:  * it under the terms of the GNU General Public License as published by
 26:  * the Free Software Foundation, either version 3 of the License, or
 27:  * (at your option) any later version.
 28:  *
 29:  * This program is distributed in the hope that it will be useful,
 30:  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 31:  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 32:  * GNU General Public License for more details.
 33:  *
 34:  * You should have received a copy of the GNU General Public License
 35:  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 36:  */
 37: final class Router implements IRouter, IDefine {
 38:     
 39:     use SingletonTrait; // Yes, it will be singleton
 40:     
 41:     const DEFAULT_CONTROLLER    = DEFAULT_CONTROLLER;
 42:     const DEFAULT_ACTION        = DEFAULT_ACTION;
 43:     private $controller         = self::DEFAULT_CONTROLLER;
 44:     private $action             = self::DEFAULT_ACTION;
 45:     private $params             = array();
 46:     
 47:     /**
 48:      * @see \Define\Core\IRouter::init()
 49:      */
 50:     public function init(array $options = array()) {
 51:         empty($options) ? $this->doSetup() : $this->get($options);
 52:     }
 53:     
 54:     /**
 55:      * Set cont
 56:      * @param array $options
 57:      */
 58:     private function get(array $options = array()) {
 59:         isset($options["controller"]) && $options['controller'] != '' ? $this->setController($options["controller"]) : CONTROLLER_NAMESPACE . $this->controller;
 60:         isset($options["action"]) ? $this->setAction($options["action"]) : $this->action;
 61:         isset($options["params"]) ? $this->setParams($options["params"]) : $this->params;
 62:     }
 63:     
 64:     /**
 65:      * @see Define\Core.IRouter::doSetup()
 66:      */
 67:     public function doSetup() {
 68:         $path = parse_url(filter_var($_SERVER["REQUEST_URI"], FILTER_SANITIZE_URL), PHP_URL_PATH);
 69:         
 70:         substr($path, -1) != NEED_SLASH ? $this->setController(ERROR_CONTROLLER) : $path = trim($path, '/');
 71:         
 72:         $path = preg_replace('/[^a-zA-Z0-9]\//', "", $path);
 73:         
 74:         @list($controller, $action, $params) = explode(SEPARATOR, $path, 3);
 75:         
 76:         !empty($controller) ? $this->setController($controller) : $this->setController($this->controller);
 77:         !empty($action) ? $this->setAction($action) : $this->setAction($this->action);
 78:         !empty($params) ? $this->setParams(explode("-", $params)) : $this->setParams($this->params);
 79:     }
 80:     
 81:     /**
 82:      * @see Define\Core.IRouter::setController()
 83:      */
 84:     public function setController($controller) {
 85:         //For the name with 2 or more words. For example - /nitesh-apte/someaction/. Controller name will be NiteshApteController
 86:         $controller = \str_replace("-", "", \mb_convert_case(\mb_strtolower($controller), \MB_CASE_TITLE, "UTF-8"));
 87:         $this->controller = CONTROLLER_NAMESPACE . CONTROLLER_PREFIX . $controller . CONTROLLER_SUFFIX;
 88:         $file = CONTROLLER_PATH . CONTROLLER_PREFIX . $controller . CONTROLLER_SUFFIX.'.php';
 89: 
 90:         if (!\file_exists($file)) {
 91:             $this->controller = CONTROLLER_NAMESPACE . CONTROLLER_PREFIX . ERROR_CONTROLLER . CONTROLLER_SUFFIX;
 92:         }
 93:         return $this;
 94:     }
 95:     
 96:     /**
 97:      * @see Define\Core.IRouter::setAction()
 98:      */
 99:     public function setAction($action) {
100:         $action = str_replace("-", "", \mb_convert_case(\mb_strtolower($action), \MB_CASE_TITLE, "UTF-8"));
101:         $reflector = new \ReflectionClass($this->controller);
102:         $this->action = lcfirst($action . ACTION_SUFFIX);
103:         if (!$reflector->hasMethod($this->action)) {
104:             $this->controller = CONTROLLER_NAMESPACE . CONTROLLER_PREFIX . ERROR_CONTROLLER . CONTROLLER_SUFFIX;
105:             $this->action = self::DEFAULT_ACTION . ACTION_SUFFIX;
106:         }
107:         return $this;
108:     }
109:     
110:     /**
111:      * @see Define\Core.IRouter::setParams()
112:      */
113:     public function setParams(array $params) {
114:         $this->params = $params;
115:         return $this;
116:     }
117:     
118:     /**
119:      * @see Define\Core.IRouter::run()
120:      */
121:     public function run() {
122:         try {
123:             $controller = new $this->controller();
124:             if(!$controller instanceof BaseController) {
125:                 throw new FrameworkException("Controller {$this->controller} is not of type Define\Core\BaseController.", 1003);
126:             }
127:             \call_user_func_array(array($controller, $this->action), $this->params);
128:         } catch (\Exception $e) {
129:             echo $e;
130:         }       
131:     }
132: }
API documentation generated by ApiGen