简体   繁体   English

带斜杠的ZF2路由参数

[英]ZF2 route parameters with slash

Is it possible to assemble a route with parameters containing forward slashes? 是否可以使用包含正斜杠的参数组装路径?

Config: 配置:

'someroute' => array(
       'type' => 'Zend\Mvc\Router\Http\Segment',
       'options' => array(
                'route' => 'someroute/:path',
                'defaults' => array(
                    'controller' => 'Controller',
                    'action' => 'index'
                ),
                'constraints' => array(
                    'path' => '(.)+'
                )
       )
 )

Controller: 控制器:

$path = 'some/subdirectory';
$this->url('someroute', array('path' => $path));

Results in: 结果是:

http://host.name/someroute/some%2Fsubdirectory

在视图中使用rawurldecode()当然可以解决这个问题。

Just use the regex route type: 只需使用regex路由类型:

'path' => array(
    'type' => 'regex',
    'options' => array(
        'regex' => '/path(?<path>\/.*)',
        'defaults' => array(
            'controller' => 'explorer',
            'action' => 'path',
        ),
        'spec' => '/path%path%'
    )
)

I had a similar problem, so I'm posting found solution with Zend 3 to my project. 我遇到了类似的问题,所以我将Zend 3的解决方案发布到我的项目中。

By default, the Symfony/Zend Routing component requires that the parameters match the following regular expression: [^/]+. 默认情况下,Symfony / Zend Routing组件要求参数与以下正则表达式匹配:[^ /] +。 This means that all characters are allowed except /. 这意味着除了/之外允许所有字符。

You must explicitly allow / to be part of your placeholder by specifying a more permissive regular expression for it: 您必须通过为其指定更宽松的正则表达式来明确允许/成为占位符的一部分:

  'type' => Segment::class,
                'options' => [
                    'route' => '/imovel[/:id][/:realtor][/:friendly]',
                    'constraints' => array(
                        'friendly' => '.+',
                        'id' => '[0-9]+',
                        'realtor' => 'C[0-9]+'
                    ),
                    'defaults' => [
                        'controller' => Controller\PropertyController::class,
                        'action' => 'form'
                    ]
                ]

Basically, you can allow all characters, and then check/trycatch/validate in the action. 基本上,您可以允许所有字符,然后在操作中检查/ trycatch / validate。

Ref: How to Allow a "/" Character in a Route Parameter 参考: 如何在路径参数中允许“/”字符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM