问题
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