/** * @inheritdoc */ public function isMatched(RequestContext $requestContext) { if (!preg_match('!^/my/!', $requestContext->getPath())) { return false; } $requestContext->setPath(preg_replace('!^/my!', '', $requestContext->getPath())); }
/** * @inheritdoc */ public function isMatched(\ReRoute\RequestContext $requestContext) { $prefix = $this->getPrefix(); if (empty($prefix)) { return true; } if (strpos($requestContext->getPath(), $prefix) === 0) { $pathWithoutPrefix = (string) substr($requestContext->getPath(), strlen($prefix)); if (substr($pathWithoutPrefix, 0, 1) != '/') { $pathWithoutPrefix = '/' . $pathWithoutPrefix; } $requestContext->setPath($pathWithoutPrefix); return true; } return false; }
public function testRequestContext() { $requestContext = new RequestContext('GET', 'example.com', 'http', '/', 'a=1&b=2'); $this->assertEquals('example.com', $requestContext->getHost()); $requestContext->setHost('www.example.com'); $this->assertEquals('www.example.com', $requestContext->getHost()); $this->assertEquals('get', $requestContext->getMethod()); $requestContext->setMethod('post'); $this->assertEquals('post', $requestContext->getMethod()); $this->assertEquals('/', $requestContext->getPath()); $requestContext->setPath('/index/'); $this->assertEquals('http', $requestContext->getScheme()); $requestContext->setScheme('https'); $this->assertEquals('https', $requestContext->getScheme()); $this->assertTrue($requestContext->hasParameter('a')); $this->assertEquals(1, $requestContext->getParameter('a')); $this->assertEquals(2, $requestContext->getParameter('b')); $this->assertEquals(['a' => 1, 'b' => 2], $requestContext->getParameters()); $this->assertNull($requestContext->getParameter('c')); $requestContext->setParameter('c', 3); $this->assertEquals(3, $requestContext->getParameter('c')); $this->assertEquals('a=1&b=2&c=3', $requestContext->getQueryString()); $requestContext->setParameter('c', 4)->removeParameter('a')->setParameter('b', 5); $this->assertNull($requestContext->getParameter('a')); $this->assertEquals('b=5&c=4', $requestContext->getQueryString()); $this->assertEquals(['b' => 5, 'c' => 4], $requestContext->getParameters()); $requestContext->setParameters(['a' => 1, 'b' => 2]); $this->assertEquals(['a' => 1, 'b' => 2], $requestContext->getParameters()); $this->assertEquals('a=1&b=2', $requestContext->getQueryString()); }
/** * @param RequestContext $requestContext * @return bool */ public function isMatched(RequestContext $requestContext) { if (!empty($this->pathTemplate)) { if ($this->pathTemplate->match($requestContext->getPath(), $matchedParams)) { $this->storeDefaultParameters($matchedParams); } else { return false; } } if (!empty($this->hostTemplate)) { if ($this->hostTemplate->match($requestContext->getHost(), $matchedParams)) { $this->storeParameters($matchedParams); } else { return false; } } if (!empty($this->scheme)) { if (!in_array($requestContext->getScheme(), explode('|', $this->scheme))) { return false; } } if (!empty($this->method)) { if (!in_array(strtolower($requestContext->getMethod()), explode('|', $this->method))) { return false; } } return true; }
/** * @param RequestContext $requestContext * * @return bool */ protected function isMatched(RequestContext $requestContext) { if ($requestContext->getHost() != 'admin.example.com') { return false; } $pathParts = explode('/', trim($requestContext->getPath(), '/')); $controllerGroup = !empty($pathParts[0]) ? $pathParts[0] : $this->defaultControllerGroup; $controllerItem = !empty($pathParts[1]) ? $pathParts[1] : $this->defaultControllerItem; $action = !empty($pathParts[2]) ? $pathParts[2] : $this->defaultAction; $this->storeParameter('controller', $controllerGroup . '/' . $controllerItem); $this->storeParameter('action', $action); return true; }
/** * @param RequestContext $requestContext * * @return bool */ public function isMatched(RequestContext $requestContext) { if (!empty($this->languagesIds)) { if (preg_match('!^/(' . implode('|', $this->languagesIds) . ')(/.*)$!', $requestContext->getPath(), $match)) { $this->storeDefaultParameter('lang', $match[1]); $newPath = $match[2]; if (empty($newPath)) { $newPath = '/'; } $requestContext->setPath($newPath); } else { $this->storeDefaultParameter('lang', $this->defaultLanguage); } } return true; }