public function routing() { $live = Live::getInstance(); // Getting arguments if (!empty($live->_request[2])) { for ($i = 2; $i < count($live->_request); $i++) { $live->_args[] = $live->_request[$i]; } } // Finding modules actions if (!empty($live->_request[0])) { $controller_name = $live->_request[0]; $controller_name[0] = strtoupper($controller_name[0]); $action_name = !empty($live->_request[1]) ? $live->_request[1] : 'index'; $module = new \Modules\Module($controller_name, true); if ($module->checkActionExists($action_name . 'Action')) { $live->_controllers[] = $controller_name; $live->_actions[] = $action_name; } } // Finding routes $router = new \Router\Router(); $findRoute = $router->findByRequest($live->_request); if ($findRoute) { $live->_controllers[] = $findRoute->controller; $live->_actions[] = $findRoute->action; } // Finding pages $page = new \Pages\Page(); $findPage = $page->findByRequest($live->_request); if ($findPage) { $live->_controllers[] = 'Pages'; $live->_actions[] = 'index'; } }
/** * Generuje link. * @param $name * @param null $data * @return bool|string */ public function generateUrl($name, $data = null) { $router = new \Router\Router('http://' . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]); $collection = $router->getCollection(); $route = $collection->get($name); if (isset($route)) { return $route->geneRateUrl($data); } return false; }
/** * 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]); } }
<?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'; });
/** * 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]); } }
<?php require __DIR__ . '/../vendor/autoload.php'; $router = Router\Router::getInstance(); var_dump($router->getRoute());
<?php session_start(); set_include_path('scripts' . PATH_SEPARATOR . get_include_path()); require_once 'config.php'; include 'autoloader.php'; require_once 'config-router.php'; $router = new \Router\Router('http://' . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]); $router->run(); $file = $router->getFile(); $classController = $router->getClass(); $method = $router->getMethod(); require_once $file; $obj = new $classController(); $obj->{$method}();
<?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(); }
<?php include 'src/Router/Route.php'; include 'src/Router/Router.php'; $router = new Router\Router('/demos/php-router'); $router->add('/', function () { global $router; ?> <h1>PHP Router</h1> <a href="<?php echo $router->url('/hola-mundo'); ?> ">Hola, Mundo!</a> <?php if (isset($_GET['example'])) { ?> <pre><code>$_GET['example'] = <?php echo htmlspecialchars($_GET['example']); ?> </code></pre> <?php } ?> <?php }); $router->add('/hola-mundo', function () { echo '<h1>Hola, mundo!</h1>'; }); $router->add('/hola-([a-zA-Z-]+)', function ($nombre) { $nombre = str_replace('-', ' ', $nombre); echo "<h1>Hola, {$nombre}</h1>";
<?php require 'vendor/autoload.php'; $router = new Router\Router('/php-router'); $router->add('/hola/([a-zA-Z]+)', function ($name) { echo sprintf('<h1>Hola %s</h1>', $name); }); $router->add('/.*', function () { header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found"); echo '<h1>404 - El sitio solicitado no existe</h1>'; }); $router->route();
public function run() { $router = new Router\Router(); $route = $router->match(); $route->run(); }
<?php /** * Author: Vehsamrak * Date Created: 19.01.16 21:34 */ const ROOT_DIRECTORY = __DIR__; error_reporting(E_ALL); ini_set('display_errors', 1); spl_autoload_register(function ($className) { $className = str_replace("\\", "/", $className); $namespace = str_replace("\\", "/", __NAMESPACE__) ?: '/'; $class = ROOT_DIRECTORY . "/src/" . $namespace . $className . ".php"; if (file_exists($class)) { include_once $class; } }); $router = new Router\Router(); $controller = $router->run();