Zend 2 路由器 - 可选未知参数

问题描述 投票:0回答:2

我们有样品的标准路线

            array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/application',
                    'defaults' => array(
                        '__NAMESPACE__' => 'Application\Controller',
                        'controller'    => 'Index',
                        'action'        => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'default' => array(
                        'type'    => 'Segment',
                        'options' => array(
                            'route'    => '/[:controller[/:action]]',
                            'constraints' => array(
                                'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                            ),
                            'defaults' => array(
                            ),
                        ),
                    ),
                ),
                'priority' => -1000,
            ),

它可以理解类似的 URL

/application
/application/some
/application/index/about

但它不理解类似的 URL

/application/index/about/param1/val1/param2/val2/...

在Zend1中是*,我们可以像这样将它添加到路由中

 'route'    => '/:controller/:action/*',

*之后的所有参数都尝试用斜杠分割。问题:zend 2 中有没有办法创建具有未知参数名称的路由?一种解决方案是创建自己的路线类型,但可能存在内置解决方案?

UPD

我自己写了一个 Route 类,它在 roure-end 上解析 * ,非必需参数将以 ZF1 风格解析。

<?php

namespace Engine\Mvc\Router\Http;

use Zend\I18n\Translator\TranslatorInterface as Translator;
use Zend\Mvc\Router\Exception;
use Zend\Stdlib\RequestInterface as Request;

class Segment extends \Zend\Mvc\Router\Http\Segment
{
    protected $unknownParameterParse = false;
    protected $route = null;
    public function __construct($route, array $constraints = [], array $defaults = [])
    {
        if ($route{mb_strlen($route)-1} == '*'){
            $route = mb_substr($route, 0, mb_strlen($route)-1);
            $this->unknownParameterParse = true;
        }
        $this->route = $route;
        parent::__construct($route, $constraints, $defaults);
    }

    public function assemble(array $params = [], array $options = []) {
        $path = parent::assemble($params, $options);
        if ($this->unknownParameterParse){
            $unknowns = [];

            foreach($params as $key=>$value){
                if (strpos($this->route, ':'.$key)===false ){
                    $unknowns[] = $this->encode($key) . '/'. $this->encode($value);
                }
            }
            if ($unknowns){
                $path = rtrim($path, '/').'/'.implode('/', $unknowns);
            }
        }
        return $path;
    }

    public function match(Request $request, $pathOffset = null, array $options = [])
    {
        if (!method_exists($request, 'getUri')) {
            return;
        }

        $uri  = $request->getUri();
        $path = $uri->getPath();

        $regex = $this->regex;

        if ($this->translationKeys) {
            if (!isset($options['translator']) || !$options['translator'] instanceof Translator) {
                throw new Exception\RuntimeException('No translator provided');
            }

            $translator = $options['translator'];
            $textDomain = (isset($options['text_domain']) ? $options['text_domain'] : 'default');
            $locale     = (isset($options['locale']) ? $options['locale'] : null);

            foreach ($this->translationKeys as $key) {
                $regex = str_replace('#' . $key . '#', $translator->translate($key, $textDomain, $locale), $regex);
            }
        }

        if ($pathOffset !== null) {
            $result = preg_match('(\G' . $regex . ')', $path, $matches, null, $pathOffset);
        } else {
            $result = preg_match('(^' . $regex . ($this->unknownParameterParse ? '' : '$') . ')', $path, $matches);
        }


        if (!$result) {
            return;
        }
        $matchedLength = strlen($matches[0]);
        $params        = [];

        foreach ($this->paramMap as $index => $name) {
            if (isset($matches[$index]) && $matches[$index] !== '') {
                $params[$this->decode($name)] = $this->decode($matches[$index]);
            }
        }

        /*ENGINE get not defined params*/
        if ($this->unknownParameterParse){
            $otherParams = explode("/", trim(substr($path, strlen($matches[0])), "/") );
            foreach($otherParams as $i=>$param){
                if ($i%2 == 0){
                    $pairKey = $param;
                }else{
                    $params[$pairKey] = $param;
                }
            }
        }
        /* endof get not defined params */
        return new \Zend\Mvc\Router\Http\RouteMatch(array_merge($this->defaults, $params), $matchedLength);
    }
}

chaoss88 怎么说,它完美地实现了通配符路由:我们可以使用 Segment 类型创建父路由,使用通配符类型创建子路由。但上面的类对前辈更友好一些。路线如下:

'route'    => '/core/:controller[/:action]*'

工作良好。但是,如果您使用 ZF2 路由器作为请求过滤的授权,通配符路由器会存在安全问题 - 这就是它被弃用的原因。但我认为路由器是用于 url 解析/组装,而不是用于过滤:用于过滤/验证 ZF2 有更好的解决方案。

php zend-framework zend-framework2
2个回答
0
投票

我认为通配符就是您正在寻找的:

            'child_routes' => array(
                'default' => array(
                    'type'    => 'Wildcard',
                    'options' => array(
                            'key_value_delimiter' => '/',
                            'param_delimiter' => '/'
                            )
                    ),
              ),

0
投票

我不确定这些参数应该代表什么,但从您的示例来看,您似乎可以/应该对这些参数使用查询参数而不是路由参数。您可以按如下方式发送请求:

application/index/about?param1=val1&param2=val2&...

使用这样的 url,您可以在控制器中执行以下操作来获取查询参数:

$param1 = $this->params()->fromQuery('param1');  // val1
$param1 = $this->params()->fromQuery('param2');  // val2

你可以像这样获得控制器和动作:

$controller = $this->params()->fromRoute('controller');  // index
$action = $this->params()->fromRoute('action');  // about

您无需更改路线配置中的任何内容即可使其正常工作。
另请检查这里的答案

© www.soinside.com 2019 - 2024. All rights reserved.