ZF2: Get url parameters in controller

2019-01-17 00:37发布

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?

4条回答
Bombasti
2楼-- · 2019-01-17 01:04

you can simply use this as well

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

I hope it works...

查看更多
戒情不戒烟
3楼-- · 2019-01-17 01:06

Or simply:

$slug= $this->params('slug');
查看更多
家丑人穷心不美
4楼-- · 2019-01-17 01:12

Why not this one:

$this->params()->fromRoute('slug');
查看更多
Animai°情兽
5楼-- · 2019-01-17 01:15

From here:

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

More documentation here, but it looks kind of incomplete.

查看更多
登录 后发表回答