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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10024969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:09:39  来源:igfitidea点击:

ZF2: Get url parameters in controller

phpzend-framework2zend-framework-mvc

提问by koenHuybrechts

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

我使用过 Zend Framework 1,并且使用该框架构建了一些应用程序。

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

现在,我正在试验 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',
                    ),
                ),
            ),
        ),
    ),
),

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

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

采纳答案by bububaba

From here:

这里

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

More documentation here, but it looks kind of incomplete.

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

回答by Al-Punk

Or simply:

或者干脆:

$slug= $this->params('slug');

回答by Tomasz Kuter

Why not this one:

为什么不是这个:

$this->params()->fromRoute('slug');

回答by Kdecom

you can simply use this as well

你也可以简单地使用它

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

I hope it works...

我希望它有效...