ZF2: Get url parameters in controller
Asked Answered
S

4

26

I have experienced Zend Framework 1 and I've build some apps with that framework.

Now, I'm experimenting Zend Framework 2, but I'm stuck on the url parameters. I've setup my routing like this:

// Setup for router and routes
'Zend\Mvc\Router\RouteStack' => array(
    'parameters' => array(
        'routes' => array(
            'default' => array(
                'type'    => 'Zend\Mvc\Router\Http\Segment',
                'options' => array(
                    'route'    => '/[:slug]',
                    'constraints' => array(
                    'slug' => '[a-zA-Z][a-zA-Z0-9_\/-]*'
                    ),
                'defaults' => array(
                    'controller' => 'Htmlsite\Controller\BootController',
                    'action'     => 'index',
                    'slug' => 'home'
                    ),
                ),
            ),
        'home' => array(
            'type' => 'Zend\Mvc\Router\Http\Literal',
            'options' => array(
                'route'    => '/',
                'defaults' => array(
                    'controller' => 'Htmlsite\Controller\BootController',
                    'action'     => 'index',
                    ),
                ),
            ),
        ),
    ),
),

As you can see, I've tried to make a variable slug. How can I access this variable?

Strati answered 5/4, 2012 at 8:14 Comment(1)
For archive purpose: Very complete answer here #12077626Jacquelinjacqueline
S
25

From here:

$slug = $this->getEvent()->getRouteMatch()->getParam('slug');

More documentation here, but it looks kind of incomplete.

Siemens answered 5/4, 2012 at 8:35 Comment(0)
D
55

Or simply:

$slug= $this->params('slug');
Dehorn answered 15/8, 2012 at 15:48 Comment(0)
E
27

Why not this one:

$this->params()->fromRoute('slug');
Ellissa answered 14/5, 2013 at 19:13 Comment(1)
This is the preferred method now.Grossularite
S
25

From here:

$slug = $this->getEvent()->getRouteMatch()->getParam('slug');

More documentation here, but it looks kind of incomplete.

Siemens answered 5/4, 2012 at 8:35 Comment(0)
O
20

you can simply use this as well

$this->params()->fromQuery('slug',null);
$this->params()->fromPost('slug',null);

I hope it works...

Oribel answered 28/10, 2012 at 22:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.