0

我有以下正则表达式路由,它允许我使用分页 - 解析 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/1,没有它 Zend “无法组装” URL。如何不在 URL 中显示第 1 页?

4

1 回答 1

0

最后,我设法实现了这一点,但只能通过重写 Regex assemble 方法。我只是覆盖了两种方法。

类扩展:

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

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

于 2013-06-25T16:06:44.937 回答