1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: class Mage_Core_Controller_Front_Router
29: {
30: protected $_config = null;
31:
32: public function __construct($config)
33: {
34: $this->_config = $config;
35: }
36:
37: public function getConfig()
38: {
39: return $this->_config;
40: }
41:
42: public function addRoutes(Zend_Controller_Router_Interface $router)
43: {
44: $frontName = $this->_config->getName();
45: $routeMatch = $frontName.'/:controller/:action/*';
46: $moduleName = (string)$this->_config->module;
47: $routeParams = array('module'=>$moduleName, 'controller'=>'index', 'action'=>'index', '_frontName'=>$frontName);
48: $route = new Zend_Controller_Router_Route($routeMatch, $routeParams);
49: $router->addRoute($moduleName, $route);
50:
51: return $this;
52: }
53:
54: public function getUrl($params=array())
55: {
56: static $reservedKeys = array('module'=>1, 'controller'=>1, 'action'=>1, 'array'=>1);
57:
58: if (is_string($params)) {
59: $paramsArr = explode('/', $params);
60: $params = array('controller'=>$paramsArr[0], 'action'=>$paramsArr[1]);
61: }
62:
63: $url = Mage::getBaseUrl($params);
64:
65: if (!empty($params['frontName'])) {
66: $url .= $params['frontName'].'/';
67: } else {
68: $url .= $this->_config->getName().'/';
69: }
70:
71: if (!empty($params)) {
72: $paramsStr = '';
73: foreach ($params as $key=>$value) {
74: if (!isset($reservedKeys[$key]) && '_'!==$key{0} && !empty($value)) {
75: $paramsStr .= $key.'/'.$value.'/';
76: }
77: }
78:
79: if (empty($params['controller']) && !empty($paramsStr)) {
80: $params['controller'] = 'index';
81: }
82: $url .= empty($params['controller']) ? '' : $params['controller'].'/';
83:
84: if (empty($params['action']) && !empty($paramsStr)) {
85: $params['action'] = 'index';
86: }
87: $url .= empty($params['action']) ? '' : $params['action'].'/';
88:
89: $url .= $paramsStr;
90:
91: $url .= empty($params['array']) ? '' : '?' . http_build_query($params['array']);
92: }
93:
94: return $url;
95: }
96: }
97: