datatype in request parameter $id = $this->params()->fromRoute('id');

95 views Asked by At

so far I have worked only with integer values as request parameters in urls.

Now I need to work with strings which contain spaces etc. for example: "xyz 666 888-VCT"

I send the route and parameters out of my view script:

<a href="<?= $this->url('pcb', ['action' => 'index', 'id' => $rs->Part_Number]) ?>">PCB</a>

I try to get it within my controller action:

$id = $this->params()->fromRoute('id');

How is the right way to catch a string value in this case?

edit: here additional my routing paramters:

'pcb' => [
                                'type'    => Segment::class,
                                'options' => [
                                        'route' => '/pcb[/:action[/:id]]',
                                        'constraints' => [
                                            'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                              'id'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                                        ],
                                        'defaults' => [
                                                'controller' => Controller\PcbController::class,
                                                'action'     => 'index',
                                        ],
                                ],

                        ],

I get always

the requested url could not match by routing

My url looks like follows: ...pcb/index/3441 99901 B 03 2519 xyz

I'm sure the problem are the spaces, so my question a bit more detailed, can I quote or somewhat the routing parameter?

1

There are 1 answers

0
pia-sophie On

SOLVED: my regular expression in the routing was wrong, I changed it to:

 'id'     => '[a-zA-Z0-9\-\. /]*',