Zend URL参数-隐藏键和显示值 [英] Zend URL Parameter - Hide Key and Show Value

查看:130
本文介绍了Zend URL参数-隐藏键和显示值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Zend的默认路由,URL类似于:

www.domain.com/controller/action/key1/value1/key2/value2/key3/value3

每个键和值成对存储在getParams();返回的数组中.在此示例中:

Each Key and Value are stored as a pair in the array returned by getParams(); In this example:

array("key1" => "value1", "key2" => "value2", "key3" => "value3")

我希望参数网址如下所示:

www.domain.com/controller/action/value1/value2/value3

应将参数映射到这样的数组中.键应仅取决于URL中值的位置.

The parameters should be mapped in an array like this. The key should depend just on the value's position in the URL.

array(0 => "value1", 1 => "value2", 2 => "value3")

我该怎么做?

推荐答案

您需要在

You are going to need to read up a bit on ZF Routes. But essentially what you need to do is add something like this to your Bootstrap.php:

protected function _initRoutes()
{
    $this->bootstrap('frontController');
    $frontController = $this->getResource('frontController');
    $router = $frontController->getRouter();

    $router->addRoute(
        'name_for_the_route',
        new Zend_Controller_Router_Route('controller/action/:key1/:key2/:key3', array('module' => 'default', 'controller' => 'theController', 'action' => 'theAction', 'key1' => NULL, 'key2' => NULL, 'key3' => NULL))
    );
}

NULL提供默认值.

The NULL's provide default values.

然后在您的控制器中,您将执行以下操作:

Then in your controller you will do something like this:

$key1 = $this->_request->getParam('key1');
$key2 = $this->_request->getParam('key2');
$key3 = $this->_request->getParam('key3');

或使用您之前提到的getParams方法.

or use the getParams method you previously mentioned.

您还可以使用PHP的 array_values()函数创建一个像这样的数字索引数组:

You can also use PHP's array_values() function to create a numerically indexed array like so:

$numericArray = array_values($this->_request->getParams());


养成使用路由的习惯是一个非常好的主意,因为它们提供了URI是什么以及调用了哪些控制器/动作之间的抽象.本质上,使用路由可以实现的是面向对象的代码,对于程序员来说仍然很有意义,而与此同时,对用户来说却很有意义的URI.


It is a very good idea to get into the habit of using routes as they provide abstraction between what the URI is and what controllers/actions get invoked. Essentially, what you can achieve with routes is object-oriented code that still makes perfect sense to a programmer, while at the same time a URI that makes perfect sense to a user.

这篇关于Zend URL参数-隐藏键和显示值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆