public function run() { if ($this->router->routeExists($this->request->method, $this->request->uri)) { $actionClass = $this->router->getClass($this->request->method, $this->request->uri); $action = new $actionClass($this->request); $action->run(); } else { echo 'not found :('; } }
/** * @runInSeparateProcess */ public function testRedirect() { $_SERVER['REQUEST_URI'] = '/redirect_test'; $_SERVER['REQUEST_METHOD'] = 'GET'; $router = new Router(); $router->redirect('/redirect_test', '/redirected', 301); $router->route(); $this->assertTrue(http_response_code() === 301); $headers = xdebug_get_headers(); $location_header_found = false; foreach ($headers as $header) { if (strpos($header, 'Location:') !== false) { $location_header_found = true; $this->assertTrue(strpos($header, '/redirected') !== false); } } $this->assertTrue($location_header_found); }
public function testUrlBugIndexPHP() { $_SERVER['REQUEST_URI'] = '/arg1'; $_SERVER['PHP_SELF'] = '/index.php/arg1'; $router = new Router(); $router->route('test', '/:arg1', function () { return 'rootRouteGet'; }); $result = $router->url('test', 'arg1'); $this->assertEquals($result, '/arg1'); }
<?php mb_internal_encoding("UTF-8"); require '../vendor/autoload.php'; require 'generated-conf/config.php'; require "Helpers/Helper.php"; use Router\Router; function autoload($class) { $file = str_replace('\\', '/', $class) . '.php'; if (file_exists($file)) { require_once $file; } } spl_autoload_register("autoload"); session_start(); $router = new Router(); $router->process($_SERVER['REQUEST_URI']);
<?php // error_reporting(E_ALL); // ini_set('display_errors', 'On'); use router\Router; use handler\Handlers; use handler\http\HttpStatusHandler; use handler\view\ViewHandler; use handler\http\HttpStatus; use view\TemplateView; include __DIR__ . '/../vendor/autoload.php'; // all dates in UTC timezone date_default_timezone_set("UTC"); ini_set('date.timezone', 'UTC'); $router = new Router(); $handlers = Handlers::get(); $handlers->add(new ViewHandler()); $handlers->add(new HttpStatusHandler()); /** * Fetch all posts * * @return Json array with all posts */ $router->route('encode', '/', function () { return new TemplateView(__DIR__ . '/encodeimage.html'); })->route('upload', '/', function () { $options = new upload\UploadOptions(); $options->setAllowOverwrite(true)->setMaxFiles(1)->setMaxSize(1024 * 200); // 200 KB $manager = new upload\UploadManager($options); try {
/** * Test that a url hash is not taken into account when matching the url */ public function testRouteGetWithHash() { $router = new Router(); $router->route('/test', function () { return 'rootRouteHash'; }); $result = $router->match('/test#asdf'); $this->assertEquals('rootRouteHash', $result); }
public function init() { $this->dbmanipulation = new DbManipulation($this->config["dbsettings"], $this->config["main"]["isDevelopment"]); $this->controller = new Controller(); Router::dispatch($this->controller); }
<?php use router\Router; use handler\Handlers; use handler\json\JsonHandler; use handler\http\HttpStatusHandler; use handler\http\HttpStatus; include __DIR__ . '/../vendor/autoload.php'; // all dates in UTC timezone date_default_timezone_set("UTC"); ini_set('date.timezone', 'UTC'); $router = new Router(); Handlers::get()->add(new JsonHandler()); Handlers::get()->add(new HttpStatusHandler()); /** * */ $router->route('root', '/', function () { return new HttpStatus(200); }); $result = $router->match($_SERVER['REQUEST_URI'], $_SERVER['REQUEST_METHOD']); $handler = Handlers::get()->getHandler($result); if ($handler) { $handler->handle($result); } else { $error = new HttpStatus(404, ' '); $handler = Handlers::get()->getHandler($error); $handler->handle($error); } return $result; // for testing purposes
public function test_requestController_can_dispatch_route_with_multiple_wildcards() { $this->SERVER_SUPER_GLOBAL_MOCK(["REQUEST_METHOD" => 'GET', "REQUEST_URI" => '/photos/ee343/comment/11235813213455']); $router = new Router(); $router->get('/photos/:id/comment/:id', 'Router\\RouteMock@index'); $this->assertNull($router->run()); }
<?php /** * Example use of router, all requests are redirected to index.php and serviced */ use router\Router; use exceptions\ControllerNotFound; use exceptions\ActionNotFoundException; use exceptions\NotAuthenticatedException; require_once __DIR__ . '/src/autoload.php'; define("CONTROLLER_NS", "Controller\\"); $router = new Router("/router"); // Controller = DefaultController, Action = doAction $router->addRoute("/", 'default#do', [Router::GET]); $matchAction = $router->match(); if (isset($matchAction)) { $action_array = explode("#", $matchAction['action']); $controller_str = CONTROLLER_NS . ucfirst($action_array[0]) . 'Controller'; $action_str = $action_array[1] . 'Action'; try { if (class_exists($controller_str)) { $controller = new $controller_str(); if (method_exists($controller, $action_str)) { $controller->{$action_str}(); } else { throw new ActionNotFoundException(); } } else { throw new ControllerNotFound(); } } catch (ControllerNotFound $ex) {
<?php use router\Router; include __DIR__ . '/../vendor/autoload.php'; $router = new Router(); $comment = ''; $router->route('root', '/', function () use(&$comment) { return $comment . 'Root'; })->route('args', '/:arg1', function ($arg1) use(&$comment) { return $comment . 'route with argument "' . $arg1 . '"'; }); $comment = '<h2>Routes</h2> <ul> <li><a href="' . $router->url('root') . '">root</a></li> <li><a href="' . $router->url('args', 'arg1') . '">/:arg1</a> <a href="' . $router->url('args', 'arg2') . '">/:arg1</a></li> </ul> <hr />'; echo $router->match($_SERVER['REQUEST_URI']);
use handler\Handlers; use handler\json\JsonHandler; use handler\http\HttpStatusHandler; use handler\json\Json; use handler\http\HttpStatus; use http\HttpSession; use auth\service\HttpAuth; use auth\provider\HtpasswdProvider; include __DIR__ . '/../vendor/autoload.php'; include 'Model_Post.php'; // setup database R::setup('sqlite:../db/dbfile.db'); // all dates in UTC timezone date_default_timezone_set("UTC"); ini_set('date.timezone', 'UTC'); $router = new Router(); $auth = new HttpAuth(new HtpasswdProvider('../db/.htpasswd'), 'Posts admin'); $handlers = Handlers::get(); $handlers->add(new JsonHandler()); $handlers->add(new HttpStatusHandler()); /** * Fetch all posts * * @return Json array with all posts */ $router->route('posts-list', '/posts', function () { $result = []; $posts = R::find('post', ' isActive = 1 ORDER BY created DESC'); /* @var $post RedBeanPHP\OODBBean */ foreach ($posts as $post) { $result[] = $post->export();
public function __construct(RequestAnalyzer $requestAnalyzer) { parent::__construct($requestAnalyzer); $this->applicationRoot = new ApplicationRoot(); }
<?php error_reporting(E_ALL); ini_set('display_errors', 'On'); use router\Router; use handler\Handlers; use handler\http\HttpStatusHandler; use handler\view\ViewHandler; use handler\http\HttpStatus; use view\TemplateView; include __DIR__ . '/../vendor/autoload.php'; // all dates in UTC timezone date_default_timezone_set("UTC"); ini_set('date.timezone', 'UTC'); $router = new Router(); $handlers = Handlers::get(); $handlers->add(new ViewHandler()); $handlers->add(new HttpStatusHandler()); $router->route('/', '/', function () { return new TemplateView(__DIR__ . '/image/controllers.html'); }); $result = $router->match($_SERVER['REQUEST_URI'], $_SERVER['REQUEST_METHOD']); $handler = $handlers->getHandler($result); if ($handler) { $handler->handle($result); } else { $error = new HttpStatus(404, ' '); $handler = $handlers->getHandler($error); $handler->handle($error); }
<?php require __DIR__ . "/../Psr4Autoloader.php"; $autoloader = new Psr4Autoloader(); $autoloader->register(); $autoloader->addNamespace("Container", __DIR__ . "/../lib/Container"); $autoloader->addNamespace("Router", __DIR__ . "/../lib/Router"); $autoloader->addNamespace("Actions", __DIR__ . "/../src/Actions"); $autoloader->loadClass('Container\\Container'); use Container\Container; use Router\Router; use Router\RouteCollection; use Router\Route; use Actions\AbstractAction; use Router\RouteNotFoundException; $container = new Container(); $routes = new RouteCollection(); $routes->add('home', new Route('/', array('_action' => 'Actions\\HomeAction'))); $router = new Router($routes); try { $route = $router->resolve($_SERVER['REQUEST_URI']); } catch (RouteNotFoundException $e) { $route = new Route('error404', array('_action' => 'Actions\\Error404Action')); } $actionClass = $route->getDefault('_action'); /** @var AbstractAction $action */ $action = new $actionClass($container); echo $action->render();