Rewrite.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Controller
  17. * @subpackage Router
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @version $Id$
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. /** Zend_Controller_Router_Abstract */
  23. require_once 'Zend/Controller/Router/Abstract.php';
  24. /** Zend_Controller_Router_Route */
  25. require_once 'Zend/Controller/Router/Route.php';
  26. /**
  27. * Ruby routing based Router.
  28. *
  29. * @package Zend_Controller
  30. * @subpackage Router
  31. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. * @see http://manuals.rubyonrails.com/read/chapter/65
  34. */
  35. class Zend_Controller_Router_Rewrite extends Zend_Controller_Router_Abstract
  36. {
  37. /**
  38. * Whether or not to use default routes
  39. *
  40. * @var boolean
  41. */
  42. protected $_useDefaultRoutes = true;
  43. /**
  44. * Array of routes to match against
  45. *
  46. * @var array
  47. */
  48. protected $_routes = array();
  49. /**
  50. * Currently matched route
  51. *
  52. * @var Zend_Controller_Router_Route_Interface
  53. */
  54. protected $_currentRoute = null;
  55. /**
  56. * Global parameters given to all routes
  57. *
  58. * @var array
  59. */
  60. protected $_globalParams = array();
  61. /**
  62. * Add default routes which are used to mimic basic router behaviour
  63. *
  64. * @return Zend_Controller_Router_Rewrite
  65. */
  66. public function addDefaultRoutes()
  67. {
  68. if (!$this->hasRoute('default')) {
  69. $dispatcher = $this->getFrontController()->getDispatcher();
  70. $request = $this->getFrontController()->getRequest();
  71. require_once 'Zend/Controller/Router/Route/Module.php';
  72. $compat = new Zend_Controller_Router_Route_Module(array(), $dispatcher, $request);
  73. $this->_routes = array_merge(array('default' => $compat), $this->_routes);
  74. }
  75. return $this;
  76. }
  77. /**
  78. * Add route to the route chain
  79. *
  80. * If route contains method setRequest(), it is initialized with a request object
  81. *
  82. * @param string $name Name of the route
  83. * @param Zend_Controller_Router_Route_Interface $route Instance of the route
  84. * @return Zend_Controller_Router_Rewrite
  85. */
  86. public function addRoute($name, Zend_Controller_Router_Route_Interface $route)
  87. {
  88. if (method_exists($route, 'setRequest')) {
  89. $route->setRequest($this->getFrontController()->getRequest());
  90. }
  91. $this->_routes[$name] = $route;
  92. return $this;
  93. }
  94. /**
  95. * Add routes to the route chain
  96. *
  97. * @param array $routes Array of routes with names as keys and routes as values
  98. * @return Zend_Controller_Router_Rewrite
  99. */
  100. public function addRoutes($routes) {
  101. foreach ($routes as $name => $route) {
  102. $this->addRoute($name, $route);
  103. }
  104. return $this;
  105. }
  106. /**
  107. * Create routes out of Zend_Config configuration
  108. *
  109. * Example INI:
  110. * routes.archive.route = "archive/:year/*"
  111. * routes.archive.defaults.controller = archive
  112. * routes.archive.defaults.action = show
  113. * routes.archive.defaults.year = 2000
  114. * routes.archive.reqs.year = "\d+"
  115. *
  116. * routes.news.type = "Zend_Controller_Router_Route_Static"
  117. * routes.news.route = "news"
  118. * routes.news.defaults.controller = "news"
  119. * routes.news.defaults.action = "list"
  120. *
  121. * And finally after you have created a Zend_Config with above ini:
  122. * $router = new Zend_Controller_Router_Rewrite();
  123. * $router->addConfig($config, 'routes');
  124. *
  125. * @param Zend_Config $config Configuration object
  126. * @param string $section Name of the config section containing route's definitions
  127. * @throws Zend_Controller_Router_Exception
  128. * @return Zend_Controller_Router_Rewrite
  129. */
  130. public function addConfig(Zend_Config $config, $section = null)
  131. {
  132. if ($section !== null) {
  133. if ($config->{$section} === null) {
  134. require_once 'Zend/Controller/Router/Exception.php';
  135. throw new Zend_Controller_Router_Exception("No route configuration in section '{$section}'");
  136. }
  137. $config = $config->{$section};
  138. }
  139. foreach ($config as $name => $info) {
  140. $route = $this->_getRouteFromConfig($info);
  141. if ($route instanceof Zend_Controller_Router_Route_Chain) {
  142. if (!isset($info->chain)) {
  143. require_once 'Zend/Controller/Router/Exception.php';
  144. throw new Zend_Controller_Router_Exception("No chain defined");
  145. }
  146. if ($info->chain instanceof Zend_Config) {
  147. $childRouteNames = $info->chain;
  148. } else {
  149. $childRouteNames = explode(',', $info->chain);
  150. }
  151. foreach ($childRouteNames as $childRouteName) {
  152. $childRoute = $this->getRoute(trim($childRouteName));
  153. $route->chain($childRoute);
  154. }
  155. $this->addRoute($name, $route);
  156. } elseif (isset($info->chains) && $info->chains instanceof Zend_Config) {
  157. $this->_addChainRoutesFromConfig($name, $route, $info->chains);
  158. } else {
  159. $this->addRoute($name, $route);
  160. }
  161. }
  162. return $this;
  163. }
  164. /**
  165. * Get a route frm a config instance
  166. *
  167. * @param Zend_Config $info
  168. * @return Zend_Controller_Router_Route_Interface
  169. */
  170. protected function _getRouteFromConfig(Zend_Config $info)
  171. {
  172. $class = (isset($info->type)) ? $info->type : 'Zend_Controller_Router_Route';
  173. if (!class_exists($class)) {
  174. require_once 'Zend/Loader.php';
  175. Zend_Loader::loadClass($class);
  176. }
  177. $route = call_user_func(array($class, 'getInstance'), $info);
  178. if (isset($info->abstract) && $info->abstract && method_exists($route, 'isAbstract')) {
  179. $route->isAbstract(true);
  180. }
  181. return $route;
  182. }
  183. /**
  184. * Add chain routes from a config route
  185. *
  186. * @param string $name
  187. * @param Zend_Controller_Router_Route_Interface $route
  188. * @param Zend_Config $childRoutesInfo
  189. * @return void
  190. */
  191. protected function _addChainRoutesFromConfig($name,
  192. Zend_Controller_Router_Route_Interface $route,
  193. Zend_Config $childRoutesInfo)
  194. {
  195. foreach ($childRoutesInfo as $childRouteName => $childRouteInfo) {
  196. if (is_string($childRouteInfo)) {
  197. $childRouteName = $childRouteInfo;
  198. $childRoute = $this->getRoute($childRouteName);
  199. } else {
  200. $childRoute = $this->_getRouteFromConfig($childRouteInfo);
  201. }
  202. if ($route instanceof Zend_Controller_Router_Route_Chain) {
  203. $chainRoute = clone $route;
  204. $chainRoute->chain($childRoute);
  205. } else {
  206. $chainRoute = $route->chain($childRoute);
  207. }
  208. $chainName = $name . '-' . $childRouteName;
  209. if (isset($childRouteInfo->chains)) {
  210. $this->_addChainRoutesFromConfig($chainName, $chainRoute, $childRouteInfo->chains);
  211. } else {
  212. $this->addRoute($chainName, $chainRoute);
  213. }
  214. }
  215. }
  216. /**
  217. * Remove a route from the route chain
  218. *
  219. * @param string $name Name of the route
  220. * @throws Zend_Controller_Router_Exception
  221. * @return Zend_Controller_Router_Rewrite
  222. */
  223. public function removeRoute($name)
  224. {
  225. if (!isset($this->_routes[$name])) {
  226. require_once 'Zend/Controller/Router/Exception.php';
  227. throw new Zend_Controller_Router_Exception("Route $name is not defined");
  228. }
  229. unset($this->_routes[$name]);
  230. return $this;
  231. }
  232. /**
  233. * Remove all standard default routes
  234. *
  235. * @param Zend_Controller_Router_Route_Interface Route
  236. * @return Zend_Controller_Router_Rewrite
  237. */
  238. public function removeDefaultRoutes()
  239. {
  240. $this->_useDefaultRoutes = false;
  241. return $this;
  242. }
  243. /**
  244. * Check if named route exists
  245. *
  246. * @param string $name Name of the route
  247. * @return boolean
  248. */
  249. public function hasRoute($name)
  250. {
  251. return isset($this->_routes[$name]);
  252. }
  253. /**
  254. * Retrieve a named route
  255. *
  256. * @param string $name Name of the route
  257. * @throws Zend_Controller_Router_Exception
  258. * @return Zend_Controller_Router_Route_Interface Route object
  259. */
  260. public function getRoute($name)
  261. {
  262. if (!isset($this->_routes[$name])) {
  263. require_once 'Zend/Controller/Router/Exception.php';
  264. throw new Zend_Controller_Router_Exception("Route $name is not defined");
  265. }
  266. return $this->_routes[$name];
  267. }
  268. /**
  269. * Retrieve a currently matched route
  270. *
  271. * @throws Zend_Controller_Router_Exception
  272. * @return Zend_Controller_Router_Route_Interface Route object
  273. */
  274. public function getCurrentRoute()
  275. {
  276. if (!isset($this->_currentRoute)) {
  277. require_once 'Zend/Controller/Router/Exception.php';
  278. throw new Zend_Controller_Router_Exception("Current route is not defined");
  279. }
  280. return $this->getRoute($this->_currentRoute);
  281. }
  282. /**
  283. * Retrieve a name of currently matched route
  284. *
  285. * @throws Zend_Controller_Router_Exception
  286. * @return Zend_Controller_Router_Route_Interface Route object
  287. */
  288. public function getCurrentRouteName()
  289. {
  290. if (!isset($this->_currentRoute)) {
  291. require_once 'Zend/Controller/Router/Exception.php';
  292. throw new Zend_Controller_Router_Exception("Current route is not defined");
  293. }
  294. return $this->_currentRoute;
  295. }
  296. /**
  297. * Retrieve an array of routes added to the route chain
  298. *
  299. * @return array All of the defined routes
  300. */
  301. public function getRoutes()
  302. {
  303. return $this->_routes;
  304. }
  305. /**
  306. * Find a matching route to the current PATH_INFO and inject
  307. * returning values to the Request object.
  308. *
  309. * @throws Zend_Controller_Router_Exception
  310. * @return Zend_Controller_Request_Abstract Request object
  311. */
  312. public function route(Zend_Controller_Request_Abstract $request)
  313. {
  314. if (!$request instanceof Zend_Controller_Request_Http) {
  315. require_once 'Zend/Controller/Router/Exception.php';
  316. throw new Zend_Controller_Router_Exception('Zend_Controller_Router_Rewrite requires a Zend_Controller_Request_Http-based request object');
  317. }
  318. if ($this->_useDefaultRoutes) {
  319. $this->addDefaultRoutes();
  320. }
  321. // Find the matching route
  322. foreach (array_reverse($this->_routes) as $name => $route) {
  323. // TODO: Should be an interface method. Hack for 1.0 BC
  324. if (method_exists($route, 'isAbstract') && $route->isAbstract()) {
  325. continue;
  326. }
  327. // TODO: Should be an interface method. Hack for 1.0 BC
  328. if (!method_exists($route, 'getVersion') || $route->getVersion() == 1) {
  329. $match = $request->getPathInfo();
  330. } else {
  331. $match = $request;
  332. }
  333. if ($params = $route->match($match)) {
  334. $this->_setRequestParams($request, $params);
  335. $this->_currentRoute = $name;
  336. break;
  337. }
  338. }
  339. return $request;
  340. }
  341. protected function _setRequestParams($request, $params)
  342. {
  343. foreach ($params as $param => $value) {
  344. $request->setParam($param, $value);
  345. if ($param === $request->getModuleKey()) {
  346. $request->setModuleName($value);
  347. }
  348. if ($param === $request->getControllerKey()) {
  349. $request->setControllerName($value);
  350. }
  351. if ($param === $request->getActionKey()) {
  352. $request->setActionName($value);
  353. }
  354. }
  355. }
  356. /**
  357. * Generates a URL path that can be used in URL creation, redirection, etc.
  358. *
  359. * @param array $userParams Options passed by a user used to override parameters
  360. * @param mixed $name The name of a Route to use
  361. * @param bool $reset Whether to reset to the route defaults ignoring URL params
  362. * @param bool $encode Tells to encode URL parts on output
  363. * @throws Zend_Controller_Router_Exception
  364. * @return string Resulting absolute URL path
  365. */
  366. public function assemble($userParams, $name = null, $reset = false, $encode = true)
  367. {
  368. if ($name == null) {
  369. try {
  370. $name = $this->getCurrentRouteName();
  371. } catch (Zend_Controller_Router_Exception $e) {
  372. $name = 'default';
  373. }
  374. }
  375. $params = array_merge($this->_globalParams, $userParams);
  376. $route = $this->getRoute($name);
  377. $url = $route->assemble($params, $reset, $encode);
  378. if (!preg_match('|^[a-z]+://|', $url)) {
  379. $url = rtrim($this->getFrontController()->getBaseUrl(), '/') . '/' . $url;
  380. }
  381. return $url;
  382. }
  383. /**
  384. * Set a global parameter
  385. *
  386. * @param string $name
  387. * @param mixed $value
  388. * @return Zend_Controller_Router_Rewrite
  389. */
  390. public function setGlobalParam($name, $value)
  391. {
  392. $this->_globalParams[$name] = $value;
  393. return $this;
  394. }
  395. }