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?

Stoyan Dimov
  • 5,250
  • 2
  • 28
  • 44
koenHuybrechts
  • 868
  • 4
  • 15
  • 28
  • 3
    For archive purpose: Very complete answer here http://stackoverflow.com/questions/12077126/how-to-access-route-post-get-etc-parameters-in-zend-framework-2 – leticia Jun 25 '13 at 15:03

4 Answers4

55

Or simply:

$slug= $this->params('slug');
Al-Punk
  • 3,531
  • 6
  • 38
  • 56
27

Why not this one:

$this->params()->fromRoute('slug');
Tomasz Kuter
  • 656
  • 8
  • 12
25

From here:

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

More documentation here, but it looks kind of incomplete.

bububaba
  • 2,840
  • 6
  • 25
  • 29
20

you can simply use this as well

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

I hope it works...

Kdecom
  • 728
  • 6
  • 6