/** * @test */ public function shouldAddRoutesToCollection() { $collection = new RouteCollection(); $collection->attach(new Route('GET /login', 'A->login')); $collection->attach(new Route('POST /login.html [ajax]', 'A->doLogin')); $collection->attach(new Route('POST /login/@id.json [ajax]', 'A->doLogin')); $this->assertEquals(0, $collection->key()); $this->assertEquals(3, count($collection)); $this->assertEquals(['GET'], $collection->current()->getMethods()); $collection->next(); $this->assertEquals(1, $collection->key()); $this->assertEquals(['POST'], $collection->current()->getMethods()); }
public function testMatchRouterUsingBasePath() { $collection = new RouteCollection(); $collection->attach(new Route('/users/', array('_controller' => 'PHPRouter\\Test\\SomeController::usersCreate', 'methods' => 'GET'))); $router = new Router($collection); $router->setBasePath('/localhost/webroot'); foreach ($this->serverProvider() as $server) { $_SERVER = $server; self::assertTrue((bool) $router->matchCurrentRequest()); } }
public function testMatchRouterUsingBasePath() { $collection = new RouteCollection(); $collection->attach(new Route('/users/', array('_controller' => 'PHPRouter\\Test\\SomeController::users_create', 'methods' => 'GET'))); $router = new Router($collection); $router->setBasePath('/localhost/webroot'); $_SERVER = []; $_SERVER['REQUEST_METHOD'] = 'GET'; $_SERVER['REQUEST_URI'] = '/localhost/webroot/users/'; $_SERVER['SCRIPT_NAME'] = 'index.php'; $this->assertTrue((bool) $router->matchCurrentRequest()); }
use PhpSandbox\Evaluator\Config; use PhpSandbox\Evaluator\Evaluator; use PhpSandbox\Library\LibraryRepository; use PhpSandbox\Library\LibraryService; use PhpSandbox\Snippet\SnippetException; use PhpSandbox\Snippet\SnippetRepository; use PhpSandbox\Snippet\SnippetService; // load config file $config = new Config(__DIR__ . '/../src/config.php'); $routing = new RouteCollection(); /** * get last executed script from tmp file */ $routing->attach(new Route('GET /get_last [ajax]', function () use($config) { if (file_exists($config->read('tmp_dir') . '/code.php')) { echo (new Evaluator($config))->getLastCode(); } })); /** * execute code from post */ $routing->attach(new Route('POST /execute/@phpversion.json [ajax]', ['phpversion' => 'null|[\\d\\.]+'], function ($params) use($config) { if (isset($_POST['code'])) { $code = $_POST['code']; if (!preg_match('/^<\\?php.*/', $code)) { $code = '<?php ' . $code; } $version = !empty($params['phpversion']) ? $params['phpversion'] : null; $evaluator = new Evaluator($config); $evaluator->setPHP($version); $result = $evaluator->evaluate($code);
use PhpRouter\Route; use PhpRouter\RouteCollection; use PhpRouter\Router; use PhpRouter\RouteRequest; class A { function index() { echo "I am the index!"; } static function showParams($params) { print_r($params); } } $routing = new RouteCollection(); $routing->attach(new Route('GET /', function () { echo 'Hello World'; })); $routing->attach(new Route('GET /page', function () { echo 'Some page...'; })); $routing->attach(new Route('GET /page/@id', ['id' => '\\d+'], function ($params) { echo 'Page no. ' . $params['id']; })); $routing->attach(new Route('GET /mac/@mac', ['mac' => '..:..:..:..:..:..'], function ($params) { echo 'Mac Address: ' . $params['mac']; })); $routing->attach(new Route('GET /index', 'A->index')); $routing->attach(new Route('GET /index/@data', 'A->showParams')); (new Router(new RouteRequest(), $routing))->run();