26

我体验过 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',
                    ),
                ),
            ),
        ),
    ),
),

正如你所看到的,我试图制作一个变量 slug。我怎样才能访问这个变量?

4

4 回答 4

56

或者简单地说:

$slug= $this->params('slug');
于 2012-08-15T15:48:44.510 回答
27

为什么不是这个:

$this->params()->fromRoute('slug');
于 2013-05-14T19:13:01.110 回答
26

这里

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

更多文档here,但看起来有点不完整。

于 2012-04-05T08:35:49.633 回答
19

你也可以简单地使用它

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

我希望它有效...

于 2012-10-28T22:47:59.803 回答