/** * Return the match dictionary for the incoming request. * * @return array The match dictionary. */ public function getMatchDict() { if ($this->_match_dict === null) { $path = $this->_request->getPath(); if (($pos = strpos($path, '?')) !== false) { $path = substr($path, 0, $pos); } if (!$path) { $path = '/'; } $this->_match_dict = new Horde_Support_Array($this->_mapper->match($path)); } return $this->_match_dict; }
public function getRequestConfiguration(Horde_Injector $injector) { $request = $injector->getInstance('Horde_Controller_Request'); $registry = $injector->getInstance('Horde_Registry'); $settingsFinder = $injector->getInstance('Horde_Core_Controller_SettingsFinder'); $config = $injector->createInstance('Horde_Core_Controller_RequestConfiguration'); $uri = substr($request->getPath(), strlen($registry->get('webroot', 'horde'))); $uri = trim($uri, '/'); if (strpos($uri, '/') === false) { $app = $uri; } else { list($app, ) = explode('/', $uri, 2); } $config->setApplication($app); // Check for route definitions. $fileroot = $registry->get('fileroot', $app); $routeFile = $fileroot . '/config/routes.php'; if (!file_exists($routeFile)) { $config->setControllerName('Horde_Core_Controller_NotFound'); return $config; } // Push $app onto the registry $registry->pushApp($app); // Application routes are relative only to the application. Let the // mapper know where they start. $this->_mapper->prefix = $registry->get('webroot', $app); // Set the application controller directory $this->_mapper->directory = $registry->get('fileroot', $app) . '/app/controllers'; // Load application routes. $mapper = $this->_mapper; include $routeFile; if (file_exists($fileroot . '/config/routes.local.php')) { include $fileroot . '/config/routes.local.php'; } // Match // @TODO Cache routes $path = $request->getPath(); if (($pos = strpos($path, '?')) !== false) { $path = substr($path, 0, $pos); } $match = $this->_mapper->match($path); if (isset($match['controller'])) { $config->setControllerName(Horde_String::ucfirst($app) . '_' . Horde_String::ucfirst($match['controller']) . '_Controller'); $config->setSettingsExporterName($settingsFinder->getSettingsExporterName($config->getControllerName())); } else { $config->setControllerName('Horde_Core_Controller_NotFound'); } return $config; }
/** * Check if request path matches any Routes to get the controller * * @return Mad_Controller_Base * @throws Mad_Controller_Exception */ public function recognize($request) { // pass a subset of the request environment // horde_routes_mapper for route matching $environ = array('REQUEST_METHOD' => $request->getMethod()); foreach (array('HTTP_HOST', 'SERVER_NAME', 'HTTPS') as $k) { $environ[$k] = $request->getServer($k); } $this->_mapper->environ = $environ; $path = $request->getPath(); if (substr($path, 0, 1) != '/') { $path = "/{$path}"; } $matchdata = $this->_mapper->match($path); if ($matchdata) { $hash = $this->formatMatchdata($matchdata); } if (empty($hash) || !isset($hash[':controller'])) { $msg = 'No routes in config/routes.php match the path: "' . $request->getPath() . '"'; throw new Mad_Controller_Exception($msg); return false; } $request->setPathParams($hash); // try to load the class $controllerName = $hash[':controller']; if (!class_exists($controllerName, false)) { $path = MAD_ROOT . '/app/controllers/' . $controllerName . '.php'; if (file_exists($path)) { require_once $path; } else { $msg = "The Controller \"{$controllerName}\" does not exist at " . $path; throw new Mad_Controller_Exception($msg); } } $controllerClassName = Mad_Support_Inflector::classify($controllerName); return new $controllerClassName(); }
* * @author Ben Klang <*****@*****.**> */ require_once __DIR__ . '/lib/Application.php'; $jonah = Horde_Registry::appInit('jonah', array('authentication' => 'none', 'session_control' => 'readonly')); $m = new Horde_Routes_Mapper(); require JONAH_BASE . '/config/routes.php'; if (file_exists(JONAH_BASE . '/config/routes.local.php')) { include JONAH_BASE . '/config/routes.local.php'; } $templates = Horde::loadConfiguration('templates.php', 'templates', 'jonah'); // Grab, and hopefully match, the URL $request = new Horde_Controller_Request_Http(); $url = $request->getPath(); $args = $request->getGetParams(); $result = $m->match('/' . $url); $criteria = array(); // @TODO: This should be handled by controller objects, but for now just use // a switch conditional until we move to Horde_Controller switch ($result['controller']) { case 'admin': // TODO: exit; case 'feed': // Default settings $defaults = array('format' => 'html', 'feed' => $result['feed']); // Check for the format specification if ($pos = strrpos($result['feed'], '.')) { $criteria['feed'] = substr($result['feed'], 0, $pos); $criteria['format'] = substr($result['feed'], $pos + 1); }
public function testFormattedResourceMember() { $m = new Horde_Routes_Mapper(); $m->resource('message', 'messages'); $m->createRegs(array('messages')); $path = '/messages/42.xml'; $m->environ = array('REQUEST_METHOD' => 'GET'); $this->assertEquals(array('controller' => 'messages', 'action' => 'show', 'id' => 42, 'format' => 'xml'), $m->match($path)); $m->environ = array('REQUEST_METHOD' => 'POST'); $this->assertNull($m->match($path)); $m->environ = array('REQUEST_METHOD' => 'PUT'); $this->assertEquals(array('controller' => 'messages', 'action' => 'update', 'id' => 42, 'format' => 'xml'), $m->match($path)); $m->environ = array('REQUEST_METHOD' => 'DELETE'); $this->assertEquals(array('controller' => 'messages', 'action' => 'delete', 'id' => 42, 'format' => 'xml'), $m->match($path)); }
public function testAutoControllerScan() { $hereDir = __DIR__; $controllerDir = "{$hereDir}/fixtures/controllers"; $m = new Horde_Routes_Mapper(array('directory' => $controllerDir)); $m->alwaysScan = true; $m->connect(':controller/:action/:id'); $expected = array('action' => 'index', 'controller' => 'content', 'id' => null); $this->assertEquals($expected, $m->match('/content')); $expected = array('action' => 'index', 'controller' => 'users', 'id' => null); $this->assertEquals($expected, $m->match('/users')); $expected = array('action' => 'index', 'controller' => 'admin/users', 'id' => null); $this->assertEquals($expected, $m->match('/admin/users')); }