Imbo\Router::route PHP Method

route() public method

Route the current request
public route ( Request $request )
$request Imbo\Http\Request\Request The current request
    public function route(Request $request)
    {
        $httpMethod = $request->getMethod();
        if ($httpMethod === 'BREW') {
            throw new RuntimeException('I\'m a teapot!', 418);
        }
        if (!isset(self::$supportedHttpMethods[$httpMethod])) {
            throw new RuntimeException('Unsupported HTTP method: ' . $httpMethod, 501);
        }
        $path = $request->getPathInfo();
        $matches = [];
        foreach ($this->routes as $resourceName => $route) {
            if (preg_match($route, $path, $matches)) {
                break;
            }
        }
        // Path matched no route
        if (!$matches) {
            throw new RuntimeException('Not Found', 404);
        }
        // Create and populate a route instance that we want to inject into the request
        $route = new Route();
        $route->setName($resourceName);
        // Inject all matches into the route as parameters
        foreach ($matches as $key => $value) {
            if (is_string($key)) {
                $route->set($key, $value);
            }
        }
        // Store the route in the request
        $request->setRoute($route);
    }

Usage Example

Beispiel #1
0
 /**
  * @dataProvider getValidRoutes
  * @covers Imbo\Router::route
  */
 public function testCanMatchValidRoutes($route, $resource, $user = null, $imageIdentifier = null, $extension = null)
 {
     $this->request->expects($this->once())->method('getPathInfo')->will($this->returnValue($route));
     $this->request->expects($this->once())->method('getMethod')->will($this->returnValue('GET'));
     $this->router->route($this->request);
     $route = $this->request->getRoute();
     $this->assertSame($user, $route->get('user'));
     $this->assertSame($imageIdentifier, $route->get('imageIdentifier'));
     $this->assertSame($extension, $route->get('extension'));
     $this->assertSame($resource, (string) $route);
 }
All Usage Examples Of Imbo\Router::route