Changeset 7518
- Timestamp:
- 02/16/08 22:57:54 (9 months ago)
- Files:
-
- branches/1.1/UPGRADE (modified) (2 diffs)
- branches/1.1/lib/config/config/factories.yml (modified) (1 diff)
- branches/1.1/lib/config/config/settings.yml (modified) (1 diff)
- branches/1.1/lib/config/sfFactoryConfigHandler.class.php (modified) (1 diff)
- branches/1.1/lib/controller/sfFrontWebController.class.php (modified) (1 diff)
- branches/1.1/lib/controller/sfWebController.class.php (modified) (3 diffs)
- branches/1.1/lib/request/sfWebRequest.class.php (modified) (1 diff)
- branches/1.1/lib/routing/sfNoRouting.class.php (modified) (7 diffs)
- branches/1.1/lib/routing/sfPathInfoRouting.class.php (modified) (7 diffs)
- branches/1.1/lib/routing/sfPatternRouting.class.php (modified) (11 diffs)
- branches/1.1/lib/routing/sfRouting.class.php (modified) (4 diffs)
- branches/1.1/lib/task/generator/skeleton/app/app/config/settings.yml (modified) (1 diff)
- branches/1.1/test/unit/controller/sfWebControllerTest.php (modified) (2 diffs)
- branches/1.1/test/unit/routing/sfPathInfoRoutingTest.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/1.1/UPGRADE
r7516 r7518 413 413 timeout: 1800 # session timeout in seconds 414 414 415 Routing default suffix 416 ---------------------- 417 418 The `sf_suffix` setting is not used anymore. To change the default suffix, 419 you now have to edit `factories.yml` instead of `settings.yml`, 420 and change the parameters of the `routing` factory: 415 Routing configuration 416 --------------------- 417 418 The `sf_suffix`, `sf_default_module`, and `sf_default_action` settings are not 419 used anymore. To change the default suffix, module, or action, you now have 420 to edit `factories.yml` instead of `settings.yml`, and change the parameters 421 of the `routing` factory: 421 422 422 423 [yml] … … 426 427 param: 427 428 load_configuration: true 428 suffix: . # Default suffix for generated URLs. If set to a single dot (.), no suffix is added. Possible values: .html, .php, and so on. 429 suffix: . # Default suffix for generated URLs. If set to a single dot (.), no suffix is added. Possible values: .html, .php, and so on. 430 default_module: default # Default module and action to be called when 431 default_action: index # A routing rule doesn't set it 429 432 430 433 `php.yml` configuration file branches/1.1/lib/config/config/factories.yml
r7516 r7518 47 47 load_configuration: true 48 48 suffix: . 49 default_module: default 50 default_action: index 49 51 50 52 logger: branches/1.1/lib/config/config/settings.yml
r7516 r7518 1 1 default: 2 2 .actions: 3 default_module: default # Default module and action to be called when4 default_action: index # A routing rule doesn't set it5 6 3 error_404_module: default # To be called when a 404 error is raised 7 4 error_404_action: error404 # Or when the requested URL doesn't match any route branches/1.1/lib/config/sfFactoryConfigHandler.class.php
r7516 r7518 162 162 163 163 case 'routing': 164 $instances[] = sprintf(" \$class = sfConfig::get('sf_factory_routing', '%s');\n \$this->factories['routing'] = new \$class(\$this->dispatcher, array_merge(array('auto_shutdown' => false, ' default_module' => sfConfig::get('sf_default_module'), 'default_action' => sfConfig::get('sf_default_action'), 'logging' => sfConfig::get('sf_logging_enabled')), sfConfig::get('sf_factory_routing_parameters', %s)));", $class, var_export(is_array($parameters) ? $parameters : array(), true));164 $instances[] = sprintf(" \$class = sfConfig::get('sf_factory_routing', '%s');\n \$this->factories['routing'] = new \$class(\$this->dispatcher, array_merge(array('auto_shutdown' => false, 'logging' => sfConfig::get('sf_logging_enabled')), sfConfig::get('sf_factory_routing_parameters', %s)));", $class, var_export(is_array($parameters) ? $parameters : array(), true)); 165 165 if (isset($parameters['load_configuration']) && $parameters['load_configuration']) 166 166 { branches/1.1/lib/controller/sfFrontWebController.class.php
r5139 r7518 45 45 $actionName = $request->getParameter('action'); 46 46 47 if (empty($moduleName) || empty($actionName)) 48 { 49 throw new sfError404Exception(sprintf('Empty module and/or action after parsing the URL "%s" (%s/%s).', $request->getPathInfo(), $moduleName, $actionName)); 50 } 51 47 52 // make the first request 48 53 $this->forward($moduleName, $actionName); branches/1.1/lib/controller/sfWebController.class.php
r7273 r7518 82 82 } 83 83 84 // default module85 if (!isset($parameters['module']))86 {87 $parameters['module'] = sfConfig::get('sf_default_module');88 }89 90 // default action91 if (!isset($parameters['action']))92 {93 $parameters['action'] = sfConfig::get('sf_default_action');94 }95 96 84 // routing to generate path 97 85 $url .= $this->context->getRouting()->generate($route_name, $parameters, $querydiv, $divider, $equals); … … 155 143 else 156 144 { 157 $tmp = explode('/', $url); 158 159 $params['module'] = $tmp[0]; 160 $params['action'] = isset($tmp[1]) ? $tmp[1] : sfConfig::get('sf_default_action'); 145 list($params['module'], $params['action']) = explode('/', $url); 161 146 } 162 147 … … 169 154 (.*?) # value 170 155 (?: 171 (?=&[^&=]+=) | $ # followed by another key= or the end of the string156 (?=&[^&=]+=) | $ # followed by another key= or the end of the string 172 157 ) 173 158 /x', $query_string, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE); branches/1.1/lib/request/sfWebRequest.class.php
r6975 r7518 908 908 protected function parseRequestParameters() 909 909 { 910 $parameters = array(); 911 $parameters = $this->dispatcher->filter(new sfEvent($this, 'request.filter_parameters', array('path_info' => $this->getPathInfo())), $parameters)->getReturnValue(); 912 913 if (!isset($parameters['module'])) 914 { 915 $parameters['module'] = sfConfig::get('sf_default_module', 'default'); 916 } 917 918 if (!isset($parameters['action'])) 919 { 920 $parameters['action'] = sfConfig::get('sf_default_action', 'index'); 921 } 922 923 if (empty($parameters['module']) || empty($parameters['action'])) 924 { 925 throw new sfError404Exception(sprintf('Empty module and/or action after parsing the URL "%s" (%s/%s).', $this->getPathInfo(), $parameters['module'], $parameters['action'])); 926 } 927 928 return $parameters; 910 return $this->dispatcher->filter(new sfEvent($this, 'request.filter_parameters', array('path_info' => $this->getPathInfo())), array())->getReturnValue(); 929 911 } 930 912 branches/1.1/lib/routing/sfNoRouting.class.php
r6817 r7518 20 20 { 21 21 /** 22 * Gets the internal URI for the current request. 23 * 24 * @param boolean Whether to give an internal URI with the route name (@route) 25 * or with the module/action pair 26 * 27 * @return string The current internal URI 22 * @see sfRouting 28 23 */ 29 24 public function getCurrentInternalUri($with_route_name = false) 30 25 { 31 $parameters = $_GET; 32 33 // module/action 34 $module = isset($parameters['module']) ? $parameters['module'] : $this->options['default_module']; 35 $action = isset($parameters['action']) ? $parameters['action'] : $this->options['default_action']; 26 $parameters = $this->fixDefaults(array_merge($this->defaultParameters, $_GET)); 27 $action = sprintf('%s/%s', $parameters['module'], $parameters['action']); 36 28 37 29 // other parameters … … 40 32 $parameters = count($parameters) ? '?'.http_build_query($parameters, null, '&') : ''; 41 33 42 return sprintf('%s /%s%s', $module, $action, $parameters);34 return sprintf('%s%s', $action, $parameters); 43 35 } 44 36 45 37 /** 46 * Generates a valid URLs for parameters. 47 * 48 * @param array The parameter values 49 * @param string The divider between key/value pairs 50 * @param string The equal sign to use between key and value 51 * 52 * @return string The generated URL 38 * @see sfRouting 53 39 */ 54 40 public function generate($name, $params, $querydiv = '/', $divider = '/', $equals = '/') … … 60 46 61 47 /** 62 * Parses a URL to find a matching route. 63 * 64 * Returns null if no route match the URL. 65 * 66 * @param string URL to be parsed 67 * 68 * @return array An array of parameters 48 * @see sfRouting 69 49 */ 70 50 public function parse($url) … … 74 54 75 55 /** 76 * Gets the current compiled route array. 77 * 78 * @return array The route array 56 * @see sfRouting 79 57 */ 80 58 public function getRoutes() … … 84 62 85 63 /** 86 * Sets the compiled route array. 87 * 88 * @param array The route array 89 * 90 * @return array The route array 64 * @see sfRouting 91 65 */ 92 66 public function setRoutes($routes) … … 96 70 97 71 /** 98 * Returns true if this instance has some routes. 99 * 100 * @return boolean 72 * @see sfRouting 101 73 */ 102 74 public function hasRoutes() … … 106 78 107 79 /** 108 * Clears all current routes.80 * @see sfRouting 109 81 */ 110 82 public function clearRoutes() branches/1.1/lib/routing/sfPathInfoRouting.class.php
r6817 r7518 23 23 24 24 /** 25 * Gets the internal URI for the current request. 26 * 27 * @param boolean Whether to give an internal URI with the route name (@route) 28 * or with the module/action pair 29 * 30 * @return string The current internal URI 25 * @see sfRouting 31 26 */ 32 27 public function getCurrentInternalUri($with_route_name = false) 33 28 { 34 29 $parameters = $this->currentRouteParameters; 35 $module = isset($parameters['module']) ? $parameters['module'] : $this->options['default_module'];36 $action = isset($parameters['action']) ? $parameters['action'] : $this->options['default_action'];37 30 38 31 // other parameters … … 41 34 $parameters = count($parameters) ? '?'.http_build_query($parameters, null, '&') : ''; 42 35 43 return sprintf('%s/%s%s', $ module, $action, $parameters);36 return sprintf('%s/%s%s', $this->currentRouteParameters['module'], $this->currentRouteParameters['action'], $parameters); 44 37 } 45 38 46 39 /** 47 * Generates a valid URLs for parameters. 48 * 49 * @param array The parameter values 50 * @param string The divider between key/value pairs 51 * @param string The equal sign to use between key and value 52 * 53 * @return string The generated URL 40 * @see sfRouting 54 41 */ 55 42 public function generate($name, $params, $querydiv = '/', $divider = '/', $equals = '/') 56 43 { 57 44 $url = ''; 58 $params = array_merge($this->defaultParameters, $params); 59 foreach ($params as $key => $value) 45 foreach (array_merge($this->defaultParameters, $params) as $key => $value) 60 46 { 61 47 $url .= '/'.$key.'/'.$value; … … 66 52 67 53 /** 68 * Parses a URL to find a matching route. 69 * 70 * Returns null if no route match the URL. 71 * 72 * @param string URL to be parsed 73 * 74 * @return array An array of parameters 54 * @see sfRouting 75 55 */ 76 56 public function parse($url) 77 57 { 78 $this->currentRouteParameters = array();58 $this->currentRouteParameters = $this->defaultParameters; 79 59 $array = explode('/', trim($url, '/')); 80 60 $count = count($array); … … 89 69 } 90 70 71 $this->currentRouteParameters = $this->fixDefaults($this->currentRouteParameters); 72 91 73 return $this->currentRouteParameters; 92 74 } 93 75 94 76 /** 95 * Gets the current compiled route array. 96 * 97 * @return array The route array 77 * @see sfRouting 98 78 */ 99 79 public function getRoutes() … … 103 83 104 84 /** 105 * Sets the compiled route array. 106 * 107 * @param array The route array 108 * 109 * @return array The route array 85 * @see sfRouting 110 86 */ 111 87 public function setRoutes($routes) … … 115 91 116 92 /** 117 * Returns true if this instance has some routes. 118 * 119 * @return boolean 93 * @see sfRouting 120 94 */ 121 95 public function hasRoutes() … … 125 99 126 100 /** 127 * Clears all current routes.101 * @see sfRouting 128 102 */ 129 103 public function clearRoutes() branches/1.1/lib/routing/sfPatternRouting.class.php
r7517 r7518 54 54 55 55 /** 56 * Gets the internal URI for the current request. 57 * 58 * @param boolean Whether to give an internal URI with the route name (@route) 59 * or with the module/action pair 60 * 61 * @return string The current internal URI 62 */ 63 56 * @see sfRouting 57 */ 64 58 public function getCurrentInternalUri($withRouteName = false) 65 59 { … … 69 63 } 70 64 71 $typeId = ($withRouteName)? 0 : 1;65 $typeId = $withRouteName ? 0 : 1; 72 66 73 67 if (!isset($this->currentInternalUri[$typeId])) … … 77 71 list($url, $regexp, $names, $namesHash, $defaults, $requirements, $suffix) = $this->routes[$this->currentRouteName]; 78 72 79 if ($withRouteName) 80 { 81 $internalUri = '@'.$this->currentRouteName; 82 } 83 else 84 { 85 $module = isset($parameters['module']) && $parameters['module'] ? $parameters['module'] : $this->options['default_module']; 86 $action = isset($parameters['action']) && $parameters['action'] ? $parameters['action'] : $this->options['default_action']; 87 88 $internalUri = $module.'/'.$action; 89 } 73 $internalUri = $withRouteName ? '@'.$this->currentRouteName : $parameters['module'].'/'.$parameters['action']; 90 74 91 75 $params = array(); … … 136 120 137 121 /** 138 * Gets the current compiled route array. 139 * 140 * @return array The route array 122 * @see sfRouting 141 123 */ 142 124 public function getRoutes() … … 146 128 147 129 /** 148 * Sets the compiled route array. 149 * 150 * @param array The route array 151 * 152 * @return array The route array 130 * @see sfRouting 153 131 */ 154 132 public function setRoutes($routes) … … 158 136 159 137 /** 160 * Returns true if this instance has some routes. 161 * 162 * @return boolean 138 * @see sfRouting 163 139 */ 164 140 public function hasRoutes() … … 168 144 169 145 /** 170 * Clears all current routes.146 * @see sfRouting 171 147 */ 172 148 public function clearRoutes() … … 346 322 347 323 /** 348 * Generates a valid URLs for parameters. 349 * 350 * @param array The parameter values 351 * @param string The divider between key/value pairs 352 * @param string The equal sign to use between key and value 353 * 354 * @return string The generated URL 324 * @see sfRouting 355 325 */ 356 326 public function generate($name, $params, $querydiv = '/', $divider = '/', $equals = '/') 357 327 { 328 $params = $this->fixDefaults($params); 329 358 330 // named route? 359 331 if ($name) … … 477 449 478 450 /** 479 * Parses a URL to find a matching route. 480 * 481 * Returns null if no route match the URL. 482 * 483 * @param string URL to be parsed 484 * 485 * @return array An array of parameters 451 * @see sfRouting 486 452 */ 487 453 public function parse($url) … … 509 475 510 476 list($route, $regexp, $names, $namesHash, $defaults, $requirements, $suffix) = $route; 477 $defaults = array_merge($defaults, $this->defaultParameters); 511 478 512 479 $break = false; … … 617 584 } 618 585 619 $this->currentRouteParameters = $ out;586 $this->currentRouteParameters = $this->fixDefaults($out); 620 587 621 588 return $this->currentRouteParameters; branches/1.1/lib/routing/sfRouting.class.php
r7517 r7518 22 22 $dispatcher = null, 23 23 $defaultParameters = array(), 24 $defaultModule = 'default', 25 $defaultAction = 'index', 24 26 $options = array(); 25 27 … … 49 51 $this->dispatcher = $dispatcher; 50 52 51 if ( !isset($options['default_module']))52 { 53 $ options['default_module'] = 'default';54 } 55 56 if ( !isset($options['default_action']))57 { 58 $ options['default_action'] = 'index';53 if (isset($options['default_module'])) 54 { 55 $this->defaultModule = $options['default_module']; 56 } 57 58 if (isset($options['default_action'])) 59 { 60 $this->defaultAction = $options['default_action']; 59 61 } 60 62 … … 143 145 144 146 /** 145 * Sets a default parameter for URL generation.147 * Sets a default parameter. 146 148 * 147 149 * @param string The key … … 163 165 } 164 166 167 protected function fixDefaults($arr) 168 { 169 if (empty($arr['module'])) 170 { 171 $arr['module'] = $this->defaultModule; 172 } 173 174 if (empty($arr['action'])) 175 { 176 $arr['action'] = $this->defaultAction; 177 } 178 179 return $arr; 180 } 181 165 182 /** 166 183 * Listens to the user.change_culture event. branches/1.1/lib/task/generator/skeleton/app/app/config/settings.yml
r7516 r7518 22 22 #all: 23 23 # .actions: 24 # default_module: default # Default module and action to be called when25 # default_action: index # A routing rule doesn't set it26 #27 24 # error_404_module: default # To be called when a 404 error is raised 28 25 # error_404_action: error404 # Or when the requested URL doesn't match any route branches/1.1/test/unit/controller/sfWebControllerTest.php
r4957 r7518 158 158 $controller->redirect('module/action?id=1#photos'); 159 159 $content = ob_get_clean(); 160 $t->like($content, '~http\://localhost/index.php/\? module=module&action=action&id=1#photos~', '->redirect() adds a refresh meta in the content');161 $t->like($context->getResponse()->getHttpHeader('Location'), '~http\://localhost/index.php/\? module=module&action=action&id=1#photos~', '->redirect() adds a Location HTTP header');160 $t->like($content, '~http\://localhost/index.php/\?action=action&module=module&id=1#photos~', '->redirect() adds a refresh meta in the content'); 161 $t->like($context->getResponse()->getHttpHeader('Location'), '~http\://localhost/index.php/\?action=action&module=module&id=1#photos~', '->redirect() adds a Location HTTP header'); 162 162 163 163 // ->genUrl() … … 165 165 166 166 $r = $context->getRouting(); 167 $t->is($controller->genUrl('module/action?id=4'), $controller->genUrl(array(' module' => 'module', 'action' => 'action', 'id' => 4)), '->genUrl() accepts a string or an array as its first argument');167 $t->is($controller->genUrl('module/action?id=4'), $controller->genUrl(array('action' => 'action', 'module' => 'module', 'id' => 4)), '->genUrl() accepts a string or an array as its first argument'); branches/1.1/test/unit/routing/sfPathInfoRoutingTest.php
r4957 r7518 32 32 // ->parse() 33 33 $t->diag('parse'); 34 $t->is($routing->parse(''), array( ), '->parse() parses a URL');35 $t->is($routing->parse('/foo/bar'), array(' foo' => 'bar'), '->parse() parses a URL');34 $t->is($routing->parse(''), array('module' => 'default', 'action' => 'index'), '->parse() parses a URL'); 35 $t->is($routing->parse('/foo/bar'), array('module' => 'default', 'action' => 'index', 'foo' => 'bar'), '->parse() parses a URL'); 36 36 $t->is($routing->parse('/module/foo/action/bar'), array('module' => 'foo', 'action' => 'bar'), '->parse() parses a URL'); 37 37 $t->is($routing->parse('/module/foo/action/bar/foo/bar'), array('foo' => 'bar', 'module' => 'foo', 'action' => 'bar'), '->parse() parses a URL');