本文介绍了如何从控制器内部访问路由参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是路线...

'panel-list' => array (
    'type' => 'Segment',
    'options' => array (
        'route'    => '/panel-list/:pageId[/:action]',
        'constraints' => array (
            'pageId' => '[a-z0-9_-]+'
        ),
        'defaults' => array (
            'action' => 'index',
            'controller' => 'PanelList',
            'site' => null
        ),
    ),
),

我需要在这里放什么....

What do I need to put in here....

public function indexAction()
{
    echo ???????
}

回显pageId?

推荐答案

在 zf2 的 beta5 中,它更改为更易于使用,因此您无需记住每种不同类型的不同语法.我引用:

In beta5 of zf2 it changed to easier usage so you don't need to remember different syntax for every different type. I cite:

新的参数"控制器插件.允许检索查询、发布、cookie、标头和路由参数.用法是$this->params()->fromQuery($name, $default).

所以要从路由中获取参数,您需要做的就是.

So to get a parameter from the route, all you need to do is.

$param = $this->params()->fromRoute('pageId');

这也可以通过查询 ($_GET) 和发布 ($_POST) 等来完成,如引文所述.

This can also be done with query ($_GET) and post ($_POST) etc. as the citation says.

$param = $this->params()->fromQuery('pageId');
// will match someurl?pageId=33

$param = $this->params()->fromPost('pageId');
// will match something with the name pageId from a form.

// You can also set a default value, if it's empty.
$param = $this->params()->fromRoute('key', 'defaultvalue');

示例:

$param = $this->params()->fromQuery('pageId', 55);

如果 url 是 someurl?pageId=33 $param 将保存值 33.如果 url 没有 ?pageId $param 将保持值 55

if the url is someurl?pageId=33 $param will hold the value 33.if the url doesn't have ?pageId $param will hold the value 55

这篇关于如何从控制器内部访问路由参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 16:37