Zend Regex用paginator路由反向

时间:2013-06-25 13:51:29

标签: regex zend-framework zend-controller-router

我有以下正则表达式路由,它允许我使用分页 - 解析URL并构建它:

; route "product-{brand}"
product.type = "Zend_Controller_Router_Route_Regex"
product.route = "product-([a-z\-]+)(?:/page/(\d+)/?)?"
product.defaults.module = "default"
product.defaults.controller = "products"
product.defaults.action = "product"
product.defaults.page = "1"
product.map.1 = "brand"
product.map.2 = "page"
product.reverse = "product-%s/page/%d"

一切正常,但是,我需要摆脱默认页面。目前我们正在将旧网络迁移到Zend,我们需要保留旧链接,因为当前的谷歌位置等。

使用默认的“page”,我总是得到/ page / 1,没有Zend“无法组装”URL。 如何不在URL中显示第1页?

1 个答案:

答案 0 :(得分:0)

最后,我设法实现了这一点,但只能通过重写Regex汇编方法。 我只是简单地重写了两种方法。

课程扩展名:

class Utils_Router_Regex extends Zend_Controller_Router_Route_Regex
{

    // instantiate 
    public static function getInstance(Zend_Config $config)
    { 
        $defs = ($config->defaults instanceof Zend_Config) ? $config->defaults->toArray() : array();
        $map = ($config->map instanceof Zend_Config) ? $config->map->toArray() : array();
        $reverse = (isset($config->reverse)) ? $config->reverse : null;

        return new self($config->route, $defs, $map, $reverse);
    }


// in this case, we are using $this->_reverse as array from config
    public function assemble($data = array(), $reset = false, $encode = false, $partial = false)
    {     
        //// EXTENSION PART !!!
        if( !isset( $data[(string) $this->_reverse->pagemap]) ) { // if not set url helper
            $reverse = $this->_reverse->base;
        } else { // if set in url helper
            $reverse = $this->_reverse->paginator;
        }


        /// end of extension part


        if ($reverse === null) {

            require_once 'Zend/Controller/Router/Exception.php';
            throw new Zend_Controller_Router_Exception('Cannot assemble. Reversed route is not specified.');
        }

        $defaultValuesMapped  = $this->_getMappedValues($this->_defaults, true, false);
        $matchedValuesMapped  = $this->_getMappedValues($this->_values, true, false);
        $dataValuesMapped     = $this->_getMappedValues($data, true, false);

        // handle resets, if so requested (By null value) to do so
        if (($resetKeys = array_search(null, $dataValuesMapped, true)) !== false) {
            foreach ((array) $resetKeys as $resetKey) {
                if (isset($matchedValuesMapped[$resetKey])) {
                    unset($matchedValuesMapped[$resetKey]);
                    unset($dataValuesMapped[$resetKey]);
                }
            }
        }

        // merge all the data together, first defaults, then values matched, then supplied
        $mergedData = $defaultValuesMapped;
        $mergedData = $this->_arrayMergeNumericKeys($mergedData, $matchedValuesMapped);
        $mergedData = $this->_arrayMergeNumericKeys($mergedData, $dataValuesMapped);

        if ($encode) {
            foreach ($mergedData as $key => &$value) {
                $value = urlencode($value);
            }
        }

        ksort($mergedData);

        $return = @vsprintf($reverse, $mergedData);

        if ($return === false) {
            require_once 'Zend/Controller/Router/Exception.php';
            throw new Zend_Controller_Router_Exception('Cannot assemble. Too few arguments?');
        }

        return $return;

    }

}

然后您可以定义要使用的sprintf字符串:

hodinky.type = "Utils_Router_Regex"
hodinky.route = "hodinky-([a-z\-]+)(?:/page/(\d+)/?)?"
hodinky.defaults.module = "default"
hodinky.defaults.controller = "products"
hodinky.defaults.action = "hodinky"
hodinky.map.1 = "znacka"
hodinky.map.2 = "page"
hodinky.reverse.paginator = "hodinky-%s/page/%d" ; assebmly with paginator
hodinky.reverse.base = "hodinky-%s/" ; assebmly without paginator
hodinky.reverse.pagemap = "page" ; "page" map key

如果您有更好的解决方案,请告诉我。