public function testRouteExecutor() { $mux = new \Pux\Mux(); ok($mux); $mux->add('/hello/:name', array('HelloController2', 'helloAction'), array('require' => array('name' => '\\w+'))); $mux->add('/product/:id', array('ProductController', 'itemAction')); $mux->add('/product', array('ProductController', 'listAction')); $mux->add('/foo', array('ProductController', 'fooAction')); $mux->add('/bar', array('ProductController', 'barAction')); $mux->add('/', array('ProductController', 'indexAction')); ok($r = $mux->dispatch('/')); is('index', RouteExecutor::execute($r)); ok($r = $mux->dispatch('/foo')); is('foo', RouteExecutor::execute($r)); ok($r = $mux->dispatch('/bar')); is('bar', RouteExecutor::execute($r)); // XXX: seems like a gc bug here return; $cb = function () use($mux) { $r = $mux->dispatch('/product/23'); RouteExecutor::execute($r); }; for ($i = 0; $i < 100; $i++) { call_user_func($cb); } for ($i = 0; $i < 100; $i++) { ok($r = $mux->dispatch('/product/23')); is('product item 23', RouteExecutor::execute($r)); } ok($r = $mux->dispatch('/hello/john')); is('hello john', RouteExecutor::execute($r)); }
public function call(array &$environment, array $response) { if ($route = $this->mux->dispatch($environment['PATH_INFO'])) { $path = $route[1]; $app = $route[2]; // Save the original PATH_INFO in ORIG_PATH_INFO // Note that some SAPI implementation will save // use the ORIG_PATH_INFO (not noticed yet) if (!isset($environment['ORIG_PATH_INFO'])) { $environment['ORIG_PATH_INFO'] = $environment['PATH_INFO']; } $environment['PATH_INFO'] = substr($environment['PATH_INFO'], strlen($path)); if ($app instanceof App || $app instanceof Middleware) { return $app($environment, $response); } else { return RouteExecutor::execute($route); } } return $response; }
public function testPCREPattern() { $mux = new Mux(); ok($mux); $mux->add('/hello/:name', array('HelloController', 'index')); $mux->compile("_cache.php"); $route = $mux->dispatch('/hello/John'); ok($route); ok($route[3]['vars'], 'vars'); ok($route[3]['vars']['name'], 'vars.name'); $response = RouteExecutor::execute($route); is("Hello John", $response); if (file_exists("_cache.php")) { unlink("_cache.php"); } }
<?php require '../vendor/autoload.php'; use Pux\Controller\ExpandableController; use Pux\RouteExecutor; use Pux\Mux; use Pux\Router; class MyController extends Controller { public function indexAction() { return 'MyController::indexAction()!'; } public function helloAction() { return 'MyController::helloAction()!'; } /** * @uri /foo */ public function overrideAction() { return 'MyController::overrideAction(), NOT MyController::fooAction()!'; } } $controller = new MyController(); $mux = new Mux(); $mux->mount('/', $controller->expand()); $route = $mux->dispatch($_SERVER['REQUEST_URI']); printf("Response: %s\n", RouteExecutor::execute($route));
$mux->get('', ['Module\\controllers\\WelcomeController', 'home']); $mux->get('\\404', ['Module\\controllers\\WelcomeController', 'not_found']); ##################################Employee Section ############################## ################################################################################# $mux->get('/employees', ['Module\\controllers\\EmployeesController', 'index']); $mux->get('/employee/add', ['Module\\controllers\\EmployeesController', 'add']); $mux->post('/employee/create', ['Module\\controllers\\EmployeesController', 'create']); $mux->get('/employee/edit/:id', ['Module\\controllers\\EmployeesController', 'edit'], ['require' => ['id' => '\\d+']]); $mux->post('/employee/update/:id', ['Module\\controllers\\EmployeesController', 'update'], ['require' => ['id' => '\\d+']]); $mux->get('/employee/remove/:id', ['Module\\controllers\\EmployeesController', 'destroy'], ['require' => ['id' => '\\d+']]); ##################################End Employee Section ############################## ##################################################################################### ##################################SalaryController Section ############################## ################################################################################# $mux->get('/salaries', ['Module\\controllers\\SalaryController', 'index']); $mux->get('/salary/add/:id', ['Module\\controllers\\SalaryController', 'add'], ['require' => ['id' => '\\d+']]); $mux->post('/salary/create/:id', ['Module\\controllers\\SalaryController', 'create']); $mux->get('/salary/edit/:id', ['Module\\controllers\\SalaryController', 'edit'], ['require' => ['id' => '\\d+']]); $mux->post('/salary/update/:id', ['Module\\controllers\\SalaryController', 'update'], ['require' => ['id' => '\\d+']]); $mux->get('/salary/remove/:id', ['Module\\controllers\\SalaryController', 'destroy'], ['require' => ['id' => '\\d+']]); ##################################End salary Section ############################## ##################################################################################### #dispatch result $route = $mux->dispatch(rtrim($_SERVER["REQUEST_URI"], '/')); //$strUri = strlen($_SERVER["REQUEST_URI"]) > 1 ? rtrim($_SERVER["REQUEST_URI"], '/') : '/home'; //$route = $mux->dispatch($strUri); if ($route) { echo RouteExecutor::execute($route); } else { echo renderPage('errors/404'); }