/** * @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'); }
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 { $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);
/** * 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; 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
<?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']);
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(); } return new Json($result); })->route('drafts-list', '/posts/drafts', function () use($auth) { $auth->authenticate(); $result = []; $drafts = R::find('post', ' isActive != 1 ORDER BY created DESC'); /* @var $post RedBeanPHP\OODBBean */ foreach ($drafts as $draft) { $result[] = $draft->export(); } return new Json($result); })->route('post', '/posts/:slug', function ($slug) { // find a post by ID or slug $post = R::findOne('post', ' slug = ? OR id = ?', [$slug, $slug]);
<?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); }