public function testRoutesWithSameBasePath2() { $router = new Router(); $router->route('test', '/root/:param', function ($arg1) { return $arg1; })->route('test2', '/root/:param/path/:param2', function ($arg1, $arg2) { return $arg1 . $arg2; }); $result = $router->match('/root/arg1/path/arg2'); $this->assertNotNull($result); $this->assertEquals('arg1arg2', $result); }
})->route('upload', '/', function () { $options = new upload\UploadOptions(); $options->setAllowOverwrite(true)->setMaxFiles(1)->setMaxSize(1024 * 200); // 200 KB $manager = new upload\UploadManager($options); try { $manager->validateUploads(); } catch (\upload\UploadException $ex) { return new TemplateView(__DIR__ . '/encodeimage.html', array('error' => $ex->getMessage())); } $files = $manager->getUploadedFiles(); $encoded = ''; /* @var $file \upload\UploadedFile */ if ($files->count()) { $file = $files->offsetGet(0); $encoded = base64_encode(file_get_contents($file->getPathname())); } $size = $file->getSize(); $base64size = mb_strlen($encoded); $percentage = $base64size / $size * 100; return new TemplateView(__DIR__ . '/encodeimage.html', array('encoded' => $encoded, 'encodedTruncated' => substr($encoded, 0, 10) . '...' . substr($encoded, -10), 'filename' => $file->getOriginalFilename(), 'size' => round($size / 1024, 2), 'base64size' => round($base64size / 1024, 2), 'percentage' => round($percentage, 2), 'mime' => $file->getMimeType(), 'isimage' => preg_match('#^image\\/.*#', $file->getMimeType()))); }, 'POST'); $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 /** * 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) {
/** * 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); }
<?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']);