Question

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
Était-ce utile?

La solution

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

Autres conseils

Just use the regex route type:

'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.

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top