/**
  * Dispatches the route and returns the response.
  *
  * @access  public
  * @return  \mako\http\Response
  */
 public function dispatch()
 {
     $returnValue = $this->beforeFilters();
     if (!empty($returnValue)) {
         $this->response->body($returnValue);
     } else {
         $action = $this->route->getAction();
         if ($action instanceof Closure) {
             $this->dispatchClosure($action);
         } else {
             $this->dispatchController($action);
         }
         if (!$this->skipAfterFilters) {
             $this->afterFilters();
         }
     }
     return $this->response;
 }
Example #2
0
 /**
  *
  */
 public function testGetRegex()
 {
     $route = new Route(['GET'], '/', 'FooController::fooAction');
     $this->assertSame('#^/?$#s', $route->getRegex());
     //
     $route = new Route(['GET'], '/foo', 'FooController::fooAction');
     $this->assertSame('#^/foo$#s', $route->getRegex());
     //
     $route = new Route(['GET'], '/foo/', 'FooController::fooAction');
     $this->assertSame('#^/foo/?$#s', $route->getRegex());
     //
     $route = new Route(['GET'], '/foo/bar', 'FooController::fooAction');
     $this->assertSame('#^/foo/bar$#s', $route->getRegex());
     //
     $route = new Route(['GET'], '/{id}', 'FooController::fooAction');
     $this->assertSame('#^/(?P<id>[^/]++)$#s', $route->getRegex());
     //
     $route = new Route(['GET'], '/foo/{id}', 'FooController::fooAction');
     $this->assertSame('#^/foo/(?P<id>[^/]++)$#s', $route->getRegex());
     //
     $route = new Route(['GET'], '/foo/{id}/bar', 'FooController::fooAction');
     $this->assertSame('#^/foo/(?P<id>[^/]++)/bar$#s', $route->getRegex());
     //
     $route = new Route(['GET'], '/foo/{id}/', 'FooController::fooAction');
     $this->assertSame('#^/foo/(?P<id>[^/]++)/?$#s', $route->getRegex());
     //
     $route = (new Route(['GET'], '/foo/{id}', 'FooController::fooAction'))->when(['id' => '[0-9]+']);
     $this->assertSame('#^/foo/(?P<id>[0-9]+)$#s', $route->getRegex());
 }
Example #3
0
 /**
  * Returns TRUE if the route matches the request path and FALSE if not.
  *
  * @access  protected
  * @param   \mako\http\routing\Route  $route       Route
  * @param   string                    $path        Request path
  * @param   array                     $parameters  Parameters
  * @return  boolean
  */
 protected function matches(Route $route, $path, array &$parameters = [])
 {
     if (preg_match($route->getRegex(), $path, $parameters) > 0) {
         foreach ($parameters as $key => $value) {
             if (is_int($key)) {
                 unset($parameters[$key]);
             }
         }
         return true;
     }
     return false;
 }