If a route match the request, lithium\net\http\Router::_scope will be updated according
the scope membership of the route
public static parse ( object $request ) : array | ||
$request | object | A request object containing URL and environment data. |
return | array | Returns an array of parameters specifying how the given request should be routed. The keys returned depend on the `Route` object that was matched, but typically include `'controller'` and `'action'` keys. |
/** * Compare $url with $mask. Returns true if there is a match ! * * @param mixed $url String, array or Request : url to test * @param array $mask Mask, in a Request::$params form * @return bool Yep/nope ? */ public static function match($url, array $mask) { // Multiple $url types if ($url instanceof Request) { $test = Router::parse($url); } elseif (is_string($url)) { $request = new Request(); $request->url = $url; $test = Router::parse($request); } else { $test = $url; } foreach ($mask as $key => $value) { if (!isset($test[$key])) { return false; } if (is_array($value) && !static::match($mask[$key], $test[$key])) { return false; } if (is_string($value) && strtolower($value) !== strtolower($test[$key])) { return false; } } return true; }
/** * Tests that continuation routes properly fall through and aggregate multiple route parameters. */ public function testRouteContinuations() { Router::connect('/{:locale:en|de|it|jp}/{:args}', array(), array('continue' => true)); Router::connect('/{:controller}/{:action}/{:id:[0-9]+}'); $request = new Request(array('url' => '/en/posts/view/1138')); $result = Router::process($request)->params; $expected = array('controller' => 'posts', 'action' => 'view', 'id' => '1138', 'locale' => 'en'); $this->assertEqual($expected, $result); $request = new Request(array('url' => '/en/foo/bar/baz')); $this->assertNull(Router::parse($request)); Router::reset(); Router::connect('/{:args}/{:locale:en|de|it|jp}', array(), array('continue' => true)); Router::connect('/{:controller}/{:action}/{:id:[0-9]+}'); $request = new Request(array('url' => '/posts/view/1138/en')); $result = Router::process($request)->params; $this->assertEqual($expected, $result); Router::reset(); Router::connect('/{:locale:en|de|it|jp}/{:args}', array(), array('continue' => true)); Router::connect('/', 'Pages::view'); $request = new Request(array('url' => '/en')); $result = Router::process($request)->params; $expected = array('locale' => 'en', 'controller' => 'Pages', 'action' => 'view'); $this->assertEqual($expected, $result); }
/** * Tests that routing is fully reset when calling `Router::reset()`. * * @return void */ public function testResettingRoutes() { Router::connect('/{:controller}', array('controller' => 'posts')); $this->request->url = '/hello'; $expected = array('controller' => 'hello', 'action' => 'index'); $result = Router::parse($this->request); $this->assertEqual($expected, $result->params); Router::reset(); $this->assertNull(Router::parse($this->request)); }
/** * Prepare data to display and calculate the current node. * @param array $menu The menu description. * @param array $options Options. * @return array Calulated menu. */ protected function _prepare(array $menu, array $options = array()) { $return = array(); $active = false; $current = array_filter($this->request->params); foreach ($menu as $label => $mask) { $link = array('url' => null, 'label' => is_string($label) ? $label : null, 'class' => null, 'active' => null, 'mask' => null); if (is_string($mask)) { $link['url'] = $mask; } elseif (array_intersect_key($mask, array('class' => true))) { $link = $mask + $link; } else { $link['url'] = $mask; } if (!is_string($link['url'])) { $link['url'] = Router::match($link['url']); } if (!$active) { if ($link['active']) { // Force the value $link['active'] = 'active'; } else { if ($link['mask']) { // We have a mask. Easy. $compare = array_filter($link['mask']); } else { // Only do this if we haven't found any active link yet and we haven't any mask to compare ! $request = new Request(); $request->url = $link['url']; $compare = array_filter(Router::parse($request)->params); } $link['active'] = Url::match($current, $compare) ? 'active' : ''; } $active = !empty($link['active']); } $return[] = $link; } return $return; }
<?php use app\services\OauthWeixinUserService; use app\extensions\util\HttpUserAgentUtil; use lithium\action\Response; use app\services\UserService; use lithium\net\http\Router; use lithium\action\Dispatcher; /** * 拦截登录过滤器, 检测页面访问权限. */ Dispatcher::applyFilter('run', function ($self, $params, $chain) { $router = Router::parse($params['request']); if (empty($router->params)) { return $chain->next($self, $params, $chain); } else { $router = $router->params; } if (UserService::check_auth($router)) { // 如果是微信里的页面, 则需要注入当前的微信用户信息 if (HttpUserAgentUtil::is_weixin()) { OauthWeixinUserService::set_current_oauth_user(); } return $chain->next($self, $params, $chain); } else { $login_url = SYS_PATH . '/user/login?ref_url=' . urldecode(current_url()); return new Response(array('location' => $login_url)); } });