ZF2:在控制器中获取url参数

koe*_*hts 25 php zend-framework-mvc zend-framework2

我已经体验过Zend Framework 1,并且我已经使用该框架构建了一些应用程序.

现在,我正在试验Zend Framework 2,但我坚持使用url参数.我设置了这样的路由:

// 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',
                    ),
                ),
            ),
        ),
    ),
),
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我试图制作一个变量slug.我该如何访问这个变量?

Al-*_*unk 55

或者干脆:

$slug= $this->params('slug');
Run Code Online (Sandbox Code Playgroud)


bub*_*aba 26

这里:

$slug = $this->getEvent()->getRouteMatch()->getParam('slug');
Run Code Online (Sandbox Code Playgroud)

这里有更多文档,但看起来有些不完整.


Tom*_*ter 26

为什么不这个:

$this->params()->fromRoute('slug');
Run Code Online (Sandbox Code Playgroud)

  • 这是现在的首选方法. (7认同)

Kde*_*com 19

你也可以简单地使用它

$this->params()->fromQuery('slug',null);
$this->params()->fromPost('slug',null);
Run Code Online (Sandbox Code Playgroud)

我希望它有效......