PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Core/Controller/Front/Router.php

https://bitbucket.org/andrewjleavitt/magestudy
PHP | 96 lines | 56 code | 16 blank | 24 comment | 11 complexity | 15ac460285c1b4cef66ab42692501ab0 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  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@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Core
  23. * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. class Mage_Core_Controller_Front_Router
  27. {
  28. protected $_config = null;
  29. public function __construct($config)
  30. {
  31. $this->_config = $config;
  32. }
  33. public function getConfig()
  34. {
  35. return $this->_config;
  36. }
  37. public function addRoutes(Zend_Controller_Router_Interface $router)
  38. {
  39. $frontName = $this->_config->getName();
  40. $routeMatch = $frontName.'/:controller/:action/*';
  41. $moduleName = (string)$this->_config->module;
  42. $routeParams = array('module'=>$moduleName, 'controller'=>'index', 'action'=>'index', '_frontName'=>$frontName);
  43. $route = new Zend_Controller_Router_Route($routeMatch, $routeParams);
  44. $router->addRoute($moduleName, $route);
  45. return $this;
  46. }
  47. public function getUrl($params=array())
  48. {
  49. static $reservedKeys = array('module'=>1, 'controller'=>1, 'action'=>1, 'array'=>1);
  50. if (is_string($params)) {
  51. $paramsArr = explode('/', $params);
  52. $params = array('controller'=>$paramsArr[0], 'action'=>$paramsArr[1]);
  53. }
  54. $url = Mage::getBaseUrl($params);
  55. if (!empty($params['frontName'])) {
  56. $url .= $params['frontName'].'/';
  57. } else {
  58. $url .= $this->_config->getName().'/';
  59. }
  60. if (!empty($params)) {
  61. $paramsStr = '';
  62. foreach ($params as $key=>$value) {
  63. if (!isset($reservedKeys[$key]) && '_'!==$key{0} && !empty($value)) {
  64. $paramsStr .= $key.'/'.$value.'/';
  65. }
  66. }
  67. if (empty($params['controller']) && !empty($paramsStr)) {
  68. $params['controller'] = 'index';
  69. }
  70. $url .= empty($params['controller']) ? '' : $params['controller'].'/';
  71. if (empty($params['action']) && !empty($paramsStr)) {
  72. $params['action'] = 'index';
  73. }
  74. $url .= empty($params['action']) ? '' : $params['action'].'/';
  75. $url .= $paramsStr;
  76. $url .= empty($params['array']) ? '' : '?' . http_build_query($params['array']);
  77. }
  78. return $url;
  79. }
  80. }