Exemple #1
0
 /**
  * Run the application.
  *
  * @param  boolean $exit
  * @return void
  */
 public function run($exit = true)
 {
     try {
         $this->init();
         // Trigger any app.route.pre events
         $this->trigger('app.route.pre');
         if (null !== $this->router) {
             $this->router->route();
             // Trigger any app.dispatch.post events
             $this->trigger('app.dispatch.pre');
             if ($this->router->hasController()) {
                 $controller = $this->router->getController();
                 if ($this->router->getControllerClass() == 'Closure') {
                     if ($this->router->hasRouteParams()) {
                         call_user_func_array($controller, $this->router->getRouteParams());
                     } else {
                         $controller();
                     }
                 } else {
                     $params = $this->router->hasRouteParams() ? $this->router->getRouteParams() : null;
                     $controller->dispatch($this->router->getAction(), $params);
                 }
             } else {
                 $this->router->noRouteFound($exit);
             }
             // Trigger any app.dispatch.post events
             $this->trigger('app.dispatch.post');
         }
     } catch (Exception $exception) {
         // Trigger any app.error events
         $this->trigger('app.error', ['exception' => $exception]);
     }
 }
Exemple #2
0
<?php

/**
 * Created by PhpStorm.
 * User: Ian
 * Date: 27-11-13
 * Time: 17:29
 */
require_once 'src/Router/Router.php';
require_once 'src/Router/Route.php';
$router = new \Router\Router();
$router->add('GET', '/', function () {
    echo 'index';
});
$router->add('/haha', function () {
    echo 'wildcard';
});
$router->add('/bar/[haha|nee|ja]', function ($route) {
    echo 'test';
    var_dump($route);
});
$router->add('/foo/:name', function ($route) {
    echo "Hallo, {$route->name}";
});
$router->route($_GET['q'], function () {
    echo 'error 404';
});
Exemple #3
0
 /**
  * Run the application.
  *
  * @return void
  */
 public function run()
 {
     try {
         $this->init();
         // Trigger any app.route.pre events
         $this->trigger('app.route.pre');
         if (null !== $this->router) {
             $this->router->route();
             // Trigger any app.route.post events
             $this->trigger('app.route.post');
             $controller = null;
             $action = null;
             // Get the routed controller
             if (null !== $this->router->getController()) {
                 $controller = $this->router->getControllerClass();
                 $action = $this->router->getRouteMatch()->getAction();
             }
             // Trigger any app.dispatch.post events
             $this->trigger('app.dispatch.pre');
             // If route has been found and controller exists, dispatch it
             if (null !== $controller) {
                 // If the controller is a closure
                 if ($controller instanceof \Closure) {
                     // If the controller->action has dispatch parameters
                     $params = $this->router()->getDispatchParams($this->router()->getRouteMatch()->getRoute());
                     if (null !== $params) {
                         if (!is_array($params)) {
                             $params = [$params];
                         }
                         call_user_func_array($controller, $params);
                         // Else, just dispatch it
                     } else {
                         $controller();
                     }
                     // Else, if it's a class
                 } else {
                     // If the controller->action has dispatch parameters
                     $params = $this->router()->getDispatchParams($this->router()->getRouteMatch()->getRoute());
                     if (null !== $params) {
                         if (!is_array($params)) {
                             $params = [$action, [$params]];
                         } else {
                             $params = array_merge([$action], [$params]);
                         }
                         call_user_func_array([$this->router->getController(), 'dispatch'], $params);
                         // Else, just dispatch it
                     } else {
                         $this->router->getController()->dispatch($action);
                     }
                 }
             } else {
                 $this->router->getRouteMatch()->noRouteFound();
             }
             // Trigger any app.dispatch.post events
             $this->trigger('app.dispatch.post');
         }
     } catch (Exception $exception) {
         // Trigger any app.error events
         $this->trigger('app.error', ['exception' => $exception]);
     }
 }
Exemple #4
0
<?php

include 'Router/autoload.php';
$router = new Router\Router();
// Rendu d'une page
function render($page, $variables)
{
    global $router;
    extract($variables);
    include __DIR__ . '/templates/layout.php';
}
// Page d'accueil
$router->register('home', '/', function () {
    render('home');
});
// Page de salutation
$router->register('hello', '/hello/*', function ($name) {
    render('hello', ['name' => $name]);
});
// Route par défaut dans le cas ou aucune autre n'a été trouvée
$router->fallback(function () {
    render('404');
});
try {
    $router->route();
} catch (\Exception $e) {
    echo 'Erreur: ' . $e->getMessage();
}