Example #1
0
 /**
  * Method: dispatch
  * Crawls the URL and calls the requested Controller and Action
  */
 public static function dispatch()
 {
     // Build the reuqest URL
     // taken and modified from http://stackoverflow.com/questions/5216172/getting-current-url
     $req_url = @$_SERVER["HTTPS"] == "on" ? "https://" : "http://";
     $req_url .= $_SERVER["HTTP_HOST"];
     $req_url .= $_SERVER["REQUEST_URI"];
     $req_url = static::$calledUrl = parse_url($req_url);
     // If theres no dispatcher limit dont replace anything
     if (Config::getVal('misc', 'dispatcher_limit') != "" || Config::getVal('misc', 'dispatcher_limit') != 0) {
         $request = str_replace(Config::getVal('misc', 'dispatcher_limit', true), '', $req_url['path']);
     } else {
         $request = $req_url['path'];
     }
     $request = trim($request, '/');
     //Dispatch Static Routes
     $sroutes = Router::matchRoutes($request);
     if ($sroutes) {
         static::load(Config::getVal('misc', 'controller_namespace', true) . $sroutes['class'], $sroutes['method'], $sroutes['param']);
         return;
     }
     $chunks = explode('/', $request);
     // Parse the GET Params
     $GET = array();
     $param = array();
     if (count($chunks) >= 2) {
         $GET = array_merge($GET, array_slice($chunks, 2));
     }
     if (@$req_url['query'] != "") {
         $pairs = explode('&', $req_url['query']);
         foreach ($pairs as $pair) {
             $parts = explode('=', $pair);
             $GET = array_merge($GET, $parts);
         }
     }
     for ($i = 0; $i <= count($GET) - 1; $i += 2) {
         $param[$GET[$i]] = $GET[$i + 1];
         if ($GET[$i] == "" && $GET[$i + 1] == "") {
             break;
         }
     }
     // If no controller is provided
     if ($chunks[0] == "") {
         if (static::$customIndex) {
             static::load(Config::getVal('misc', 'controller_namespace', true) . static::$customIndex['class'], static::$customIndex['method']);
         } else {
             static::load(Config::getVal('misc', 'controller_namespace', true) . 'Index', '_index', $param);
         }
         return;
     }
     $class = Config::getVal('misc', 'controller_namespace', true) . ucfirst($chunks[0]);
     // This fixes a weired bug where it tries to load a namespace without controller
     if (substr($class, -1) == "\\") {
         return;
     }
     // If a Controller but no Action is provided
     if (count($chunks) == 1 && $chunks[0] != "") {
         static::load($class, 'index', $param);
     }
     // If a Controller and an Action is provided
     if (count($chunks) >= 2) {
         $method = $chunks[1];
         static::load($class, $method, $param);
     }
 }
Example #2
0
 *
 */
namespace example;

// Your Project Basenamespace
// Load the dioxid loader
require_once 'path/to/dioxid/common/Loader.php';
use dioxid\Loader;
// setup the app namespace to use the loader on user written classes and the basepath for the loadding
// for each different base namespace in your app add an entry in the array in the scheme NAMESAPCE => BASEPATH
Loader::setup(array(__NAMESPACE__ => __DIR__));
//register the loader
Loader::register();
use dioxid\config\Config;
// We need to read the config
use dioxid\controller\Dispatcher;
// We have to dispatch the Controller/Action pair
use dioxid\controller\Router;
// We Only need this if we have static routes
use dioxid\error\ErrorHandler;
// And at least we want to have some Errorhandling ;)
Config::loadConfig(__DIR__ . '/config.ini', true);
//Load the Config, remeber to give it the full path to your config.ini
ErrorHandler::register();
// Register the Errorhandler
// Register your static routes. Take a look at dioxid\controller\Router.php for more documentation ;)
Router::registerStaticRoutes(':lang:/index', array('index' => 'test'));
// What is your default controller and your default action? By Default this is index/index!
Dispatcher::registerIndex('index', 'test');
// Lets Dispatch the Controller/Action pair ;)
Dispatcher::dispatch();