【问题标题】:ZF2: Get url parameters in controllerZF2:在控制器中获取 url 参数
【发布时间】:2012-04-05 08:14:27
【问题描述】:

我体验过 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。 如何访问此变量?

【问题讨论】:

标签: php zend-framework2 zend-framework-mvc


【解决方案1】:

或者简单地说:

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

【讨论】:

    【解决方案2】:

    为什么不是这个:

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

    【讨论】:

    • 这是现在的首选方法。
    【解决方案3】:

    来自here:

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

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

    【讨论】:

    • 谢谢!正是我需要的:-)
    【解决方案4】:

    你也可以简单地使用它

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

    我希望它有效...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-07
      • 1970-01-01
      相关资源
      最近更新 更多