带斜杠的ZF2路由参数

时间:2013-05-24 12:18:13

标签: php routes zend-framework2 zend-route slash

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

配置:

'someroute' => array(
       'type' => 'Zend\Mvc\Router\Http\Segment',
       '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

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

只需使用regex路由类型:

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

答案 2 :(得分:1)

我遇到了类似的问题,所以我将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' => Controller\PropertyController::class,
                        'action' => 'form'
                    ]
                ]

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

价: How to Allow a "/" Character in a Route Parameter

相关问题