/**
  * {@inheritdoc}
  */
 public function match(Request $request)
 {
     $this->router->handle($request->getUri()->getPath());
     if (!$this->router->wasMatched()) {
         // TODO Is it worth to validate, if route matches but the method is incompatible?
         return RouteResult::fromRouteFailure();
     }
     $matchedRoute = $this->router->getMatchedRoute();
     return RouteResult::fromRouteMatch($matchedRoute->getName(), $this->router->getNamespaceName(), $this->collectParams($matchedRoute));
 }
 /**
  * @dataProvider routesProvider
  *
  * @param $arRoute
  * @param $strUri
  * @param $strClassName
  */
 public function testRoutes($arRoute, $strUri, $strClassName)
 {
     $this->router->add($arRoute['route'], $arRoute['parameters'])->setName($arRoute['name']);
     $this->router->handle($strUri);
     $boolMatched = $this->router->wasMatched();
     $this->assertTrue($boolMatched, 'failed to match ' . $strUri);
     $strRouteName = $this->router->getMatchedRoute()->getName();
     $this->assertEquals($arRoute['name'], $strRouteName, 'matched wrong route');
     $this->setUpDispatcher();
     $this->dispatcher->dispatch();
     $strControllerClassName = $this->dispatcher->getControllerClass();
     $this->assertEquals($strClassName, $strControllerClassName, 'wrong controller class name');
 }
Example #3
0
 public function handle($path = null)
 {
     if ($path === null) {
         $path = $this->getRewriteUri();
     }
     // First attempt regular resolution.
     parent::handle($path);
     $router_route = $this->getMatchedRoute();
     if ($router_route !== NULL) {
         return $path;
     }
     $di = $this->getDI();
     $module_list = array_keys($di->get('phalcon_modules'));
     $path = trim($path, self::URI_DELIMITER);
     if ($path != '') {
         $path = explode(self::URI_DELIMITER, $path);
         if (in_array($path[0], $module_list)) {
             $this->_module = array_shift($path);
         }
         if (count($path) && !empty($path[0])) {
             $this->_controller = array_shift($path);
         }
         if (count($path) && !empty($path[0])) {
             $this->_action = array_shift($path);
         }
         if (count($path)) {
             $this->_params = $path;
         }
     }
     return $path;
 }
 /**
  * registers module according to uri given
  *
  * @return bool true if module successfully loaded; <br />
  * 				false otherwise;
  */
 public function handle()
 {
     $boolReturn = false;
     $oLogger = $this->di->getFileLogger();
     $oPreRouter = new Router();
     $oPreRouter->add('/api(.*)', array('module' => 'api'));
     $oPreRouter->add('/regular(.*)', array('module' => 'regular'));
     $oPreRouter->add('/blind(.*)', array('module' => 'blind'));
     $oPreRouter->handle();
     $strModuleName = $oPreRouter->getModuleName();
     /**
      * @type Request $oRequest
      */
     $oRequest = $this->di->getRequest();
     if (array_key_exists($strModuleName, $this->knownModules)) {
         $this->app->registerModules(array($strModuleName => $this->knownModules[$strModuleName]));
         $this->app->setDefaultModule($strModuleName);
         $boolReturn = true;
         $oLogger->debug(__CLASS__ . ': ' . $oRequest->getURI() . ' leads to module: ' . $strModuleName);
     } else {
         if (!U::isLegacy()) {
             $strMsg = 'failed to load phalcon module';
         } else {
             $strMsg = 'loading old backend';
         }
         $oLogger->debug(__CLASS__ . ': ' . $strMsg . ' for "' . $oRequest->getUri() . '"');
     }
     return $boolReturn;
 }
 public function handle($strUrl = null)
 {
     /**
      * @type \DiCustom $di
      */
     $di = Di::getDefault();
     $oLogger = $di->getFileLogger();
     $oLogger->debug('handle worked in api module for "' . $strUrl . '"');
     parent::handle($strUrl);
 }
 public function testAnnotations()
 {
     $di = new Di();
     $di['request'] = new \Phalcon\Http\Request();
     $router = new Router(false);
     $router->setDI($di);
     $loader = new ArrayRouteLoader($router);
     $loader->load(include __DIR__ . '/Fixtures/routes.php');
     $router->handle();
     $this->assertCount(3, $router->getRoutes());
     $routes = [['uri' => '/test4', 'method' => 'GET', 'controller' => 'test4', 'action' => 'test4'], ['uri' => '/test4', 'method' => 'POST', 'controller' => 'test4', 'action' => 'test4'], ['uri' => '/test5', 'method' => 'POST', 'controller' => 'test5', 'action' => 'test5'], ['uri' => '/test6', 'method' => 'GET', 'controller' => 'test6', 'action' => 'test6']];
     foreach ($routes as $route) {
         $_SERVER['REQUEST_METHOD'] = $route['method'];
         $router->handle($route['uri']);
         $this->assertEquals($router->getControllerName(), $route['controller']);
         $this->assertEquals($router->getActionName(), $route['action']);
         $this->assertEquals($router->isExactControllerName(), true);
     }
 }
Example #7
0
 /**
  * @param null $uri
  */
 public function handle($uri = null)
 {
     parent::handle($uri);
     if ($this->_wasMatched) {
         $afterMatch = $this->_matchedRoute->getAfterMatch();
         if (is_callable($afterMatch)) {
             call_user_func($afterMatch, $uri, $this->_matchedRoute);
         }
     }
 }
Example #8
0
 public function handle($uri = null)
 {
     if (empty($uri)) {
         $uri = $this->getRewriteUri();
     }
     $di = $this->getDI();
     $routeManager = $di->get('databaseRouteManager');
     $routes = $di->get('config')->get('routes', false);
     $route = $routeManager->findByUrl($uri);
     if ($route && $routes) {
         $routeParams = $routes->get($route->name, false);
         if ($routeParams) {
             $this->add($uri, ['module' => $routeParams->module, 'controller' => $routeParams->controller, 'action' => $routeParams->action, 0 => substr($uri, 1)]);
         } else {
             throw new \Exception('Database route not configured');
         }
     }
     return parent::handle($uri);
 }
Example #9
0
<?php

/**
 * Created by PhpStorm.
 * User: vlad
 * Date: 8/28/15
 * Time: 10:34 PM
 */
use Phalcon\Mvc\Router;
$router = new Router();
$router->removeExtraSlashes(true);
$router->add("/", array("controller" => "projects", "action" => "index"));
$router->add("/projects(/?index)?", array("controller" => "static", "action" => "error"));
$router->add("/404", array("controller" => "static", "action" => "error404"));
$router->add("/403", array("controller" => "static", "action" => "error403"));
$router->notFound(array("controller" => "static", "action" => "error404"));
$router->handle();
 /**
  * @group excluded
  * @dataProvider easyMismatchedProvider
  *
  * @param $strUri
  */
 public function testEasyMismatched($strUri)
 {
     $this->router->handle($strUri);
     $boolMatched = $this->router->wasMatched();
     $this->assertFalse($boolMatched, 'matched when should not');
 }
Example #11
0
 /**
  * Produce the routing parameters from the rewrite information
  *
  * @param string|null $uri
  * @throws Exception
  */
 public function handle($uri = null)
 {
     if (is_null($uri) === true) {
         $uri = $this->getRewriteUri();
     } elseif (is_string($uri) === false) {
         throw new Exception('Invalid parameter type.');
     }
     $annotationsService = null;
     if ($this->_processed === false) {
         if (is_array($this->_handlers) === true) {
             foreach ($this->_handlers as $scope) {
                 if (is_array($scope) === true) {
                     //A prefix (if any) must be in position 0
                     if (is_string($scope[0]) === true) {
                         if (Text::startsWith($uri, $scope[0]) === false) {
                             continue;
                         }
                     }
                     if (is_object($annotationsService) === false) {
                         if (is_object($this->_dependencyInjector) === false) {
                             throw new Exception("A dependency injection container is required to access the 'annotations' service");
                         }
                         $annotationsService = $this->_dependencyInjector->getShared('annotations');
                         //@note no interface validation
                     }
                     //The controller must be in position 1
                     if (strpos($scope[1], '\\') !== false) {
                         //Extract the real class name from the namespaced class
                         $classWithNamespace = get_class($handler);
                         //Extract the real class name from the namespaced class
                         //Extract the namespace from the namespaced class
                         $pos = strrpos($classWithNamespace, '\\');
                         if ($pos !== false) {
                             $namespaceName = substr($classWithNamespace, 0, $pos);
                             $controllerName = substr($classWithNamespace, $pos);
                         } else {
                             $controllerName = $classWithNamespace;
                             $namespaceName = null;
                         }
                         $this->_routePrefix = null;
                         //Check if the scope has a module associated
                         if (isset($scope[2]) === true) {
                             $moduleName = $scope[2];
                         } else {
                             $moduleName = null;
                         }
                         //Get the annotations from the class
                         $handlerAnnotations = $annotationsService->get($handler . $this->_controllerSuffix);
                         //Process class annotations
                         $classAnnotations = $handlerAnnotations->getClassAnnotations();
                         if (is_object($classAnnotations) === true) {
                             //Process class annotaitons
                             $annotations = $classAnnotations->getAnnotations();
                             if (is_array($annotations) === true) {
                                 foreach ($annotations as $annotation) {
                                     $this->processControllerAnnotation($annotation);
                                 }
                             }
                         }
                         //Process method annotations
                         $methodAnnotations = $handlerAnnotations->getMethodsAnnotations();
                         if (is_array($methodAnnotations) === true) {
                             foreach ($methodAnnotations as $method => $collection) {
                                 if (is_object($collection) === true) {
                                     $annotations = $collection->getAnnotations();
                                     foreach ($annotations as $annotation) {
                                         $this->processActionAnnotation($moduleName, $namespaceName, $controllerName, $method, $annotation);
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
         $this->_processed = true;
     }
     parent::handle($uri);
 }
<?php

/**
 * Created by PhpStorm.
 * User: rcmonitor
 * Date: 07.07.15
 * Time: 15:46
 */
use Phalcon\Di;
use Phalcon\Events\Manager;
use Phalcon\Mvc\Dispatcher;
use Phalcon\Mvc\Router;
use Phalcon\Version;
$di = new Di\FactoryDefault();
$oRouter = new Router(false);
$oRouter->setDI($di);
$oRouter->add('/:controller', array('controller' => 1, 'action' => 'index'));
$oEventManager = new Manager();
$oEventManager->attach('dispatch:beforeDispatch', function () {
    return false;
});
$oDispatcher = new Dispatcher();
$oDispatcher->setDI($di);
$oDispatcher->setEventsManager($oEventManager);
$oRouter->handle('/test');
$oDispatcher->setControllerName($oRouter->getControllerName());
$oDispatcher->setActionName($oRouter->getActionName());
$oDispatcher->dispatch();
echo $oDispatcher->getControllerClass() . PHP_EOL;
echo Version::get() . PHP_EOL;
Example #13
0
 public function handle($uri = null)
 {
     return parent::handle($uri);
 }