ZF2 route parameters with slash

被刻印的时光 ゝ 提交于 2020-01-13 04:23:28

问题


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

回答1:


Using rawurldecode() in the view solves this issue of course.




回答2:


Just use the regex route type:

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



回答3:


I had a similar problem, so I'm posting found solution with Zend 3 to my project.

By default, the Symfony/Zend Routing component requires that the parameters match the following regular expression: [^/]+. 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.

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



来源:https://stackoverflow.com/questions/16734810/zf2-route-parameters-with-slash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!