public function testMatchWithCustomNamedRegex() { $this->router->addMatchTypes(array('cId' => '[a-zA-Z]{2}[0-9](?:_[0-9]++)?')); $this->router->map('GET', '/bar/[cId:customId]', 'bar_action', 'bar_route'); $this->assertEquals(array('target' => 'bar_action', 'params' => array('customId' => 'AB1'), 'name' => 'bar_route'), $this->router->match('/bar/AB1', 'GET')); $this->assertEquals(array('target' => 'bar_action', 'params' => array('customId' => 'AB1_0123456789'), 'name' => 'bar_route'), $this->router->match('/bar/AB1_0123456789', 'GET')); $this->assertFalse($this->router->match('/some-other-thing', 'GET')); }
public function testMatchWithCustomNamedUnicodeRegex() { $pattern = '[^'; // Arabic characters $pattern .= '\\x{0600}-\\x{06FF}'; $pattern .= '\\x{FB50}-\\x{FDFD}'; $pattern .= '\\x{FE70}-\\x{FEFF}'; $pattern .= '\\x{0750}-\\x{077F}'; $pattern .= ']+'; $this->router->addMatchTypes(array('nonArabic' => $pattern)); $this->router->map('GET', '/bar/[nonArabic:string]', 'non_arabic_action', 'non_arabic_route'); $this->assertEquals(array('target' => 'non_arabic_action', 'name' => 'non_arabic_route', 'params' => array('string' => 'some-path')), $this->router->match('/bar/some-path', 'GET')); $this->assertFalse($this->router->match('/﷽', 'GET')); }
/** * Add a new pattern to the existing ones. * * @param string $alias The pattern alias. * @param string $pattern The regular expression pattern. * @return $this */ public function addPattern($alias, $pattern) { $this->altoRouter->addMatchTypes([$alias, $pattern]); return $this; }
<?php $router = new AltoRouter(); $router->addMatchTypes(['user' => '[0-9A-Za-z-_]++', 'slug' => '[0-9a-z-_]++']); $router->map('GET', '/', 'NewsController@index', 'home'); $router->map('GET', '/home', 'HomeController@index', 'homeold'); $router->map('GET', '/captcha', 'HomeController@captcha', 'captcha'); $router->map('POST', '/complaint', 'HomeController@complaint', 'complaint'); $router->map('GET|POST', '/register', 'UserController@register', 'register'); $router->map('GET|POST', '/login', 'UserController@login', 'login'); $router->map('GET|POST', '/recovery', 'UserController@recovery', 'recovery'); $router->map('GET|POST', '/reset', 'UserController@reset', 'reset'); $router->map('GET|POST', '/user/[edit|password:action]', 'UserController'); $router->map('GET|POST', '/users', 'UserController@index'); $router->map('GET', '/logout', 'UserController@logout', 'logout'); $router->map('GET', '/user/[user:login]', 'UserController@view', 'profile'); $router->map('POST', '/user/image', 'UserController@image'); $router->map('GET', '/guestbook', 'GuestbookController@index', 'guestbook'); $router->map('POST', '/guestbook/create', 'GuestbookController@create'); $router->map('GET|POST', '/guestbook/[i:id]/edit', 'GuestbookController@edit'); $router->map('GET|POST', '/guestbook/[i:id]/reply', 'GuestbookController@reply'); $router->map('POST', '/guestbook/delete', 'GuestbookController@delete'); $router->map('GET', '/forum', 'ForumController@index', 'forum'); $router->map('GET', '/forum/[i:id]', 'ForumController@forum'); $router->map('GET', '/topic/[i:id]', 'ForumController@topic'); $router->map('POST', '/topic/bookmark', 'ForumController@bookmark'); $router->map('POST', '/topic/[i:id]/create', 'ForumController@createPost'); $router->map('GET|POST', '/forum/create', 'ForumController@createTopic'); $router->map('GET|POST', '/post/[i:id]/edit', 'ForumController@editPost'); $router->map('GET', '/news', 'NewsController@index', 'news'); $router->map('GET', '/news/[i:id]', 'NewsController@view', 'news_view');
<?php require_once __DIR__ . '/Config.php'; require_once BASE_PATH . '/vendor/altorouter/altorouter/AltoRouter.php'; require_once BASE_PATH . '/app/Routes.php'; defined('BASE_PATH') or exit('No direct script access allowed'); $router = new AltoRouter(); $Routes = new Routes(); $base_path = str_replace('/web/', '', str_replace('index.php', '', $_SERVER['SCRIPT_NAME'])); if ($base_path) { $router->setBasePath($base_path); } if (count(Routes::$routes) > 0) { if (count(Routes::$match_types) > 0) { $router->addMatchTypes(Routes::$match_types); } $router->addRoutes(Routes::$routes); } $match = $router->match(); if ($match) { require_once BASE_PATH . '/core/Support/InitRouter.php'; $app_init = new InitRouter($match); } else { header('HTTP/1.0 404 Not Found'); echo 'Page Not Found <b>"404 Error"</b>'; }