带斜杠的ZF2路由参数



是否可以用包含正斜杠的参数组装路由?

配置:

'someroute' => array(
       'type' => 'ZendMvcRouterHttpSegment',
       'options' => array(
                'route' => 'someroute/:path',
                'defaults' => array(
                    'controller' => 'Controller',
                    'action' => 'index'
                ),
                'constraints' => array(
                    'path' => '(.)+'
                )
       )
 )

控制器:

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

结果:

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

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

只使用regex路由类型:

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

我有一个类似的问题,所以我在我的项目中发布了Zend 3的解决方案。

默认情况下,Symfony/Zend Routing组件要求参数匹配以下正则表达式:[^/]+。这意味着除了/.

,其他字符都可以使用。

必须显式地允许/作为占位符的一部分为它指定一个更宽松的正则表达式:

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

基本上,您可以允许所有字符,然后在操作中检查/trycatch/validate。

参考:如何在路由参数中允许"/"字符

最新更新