Php 如何使参数在ZendFramework2中起作用?
我在Php 如何使参数在ZendFramework2中起作用?,php,zend-framework,routes,zend-framework2,Php,Zend Framework,Routes,Zend Framework2,我在module.config.php中配置了一个路由来处理两个参数, 下面是module.config.php文件的内容,我只给出了路由的定义(相同的路由): 更新: 'router' => array( 'routes' => array( 'Products' => array( 'type' => 'Zend\Mvc\Router\Http\Literal',
module.config.php
中配置了一个路由来处理两个参数,
下面是module.config.php文件的内容,我只给出了路由的定义(相同的路由):
更新:
'router' => array(
'routes' => array(
'Products' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/application/admin/products[/:id]',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Admin',
'action' => 'index',
),
),
),
'productsList' => array(
'type' => 'segment',
'options' => array(
'route' => '/application/products/productsList[/:type][/:id]',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Products',
'action' => 'productsList'
),
),
),
'Emplacement' => array(
'type' => 'segment',
'options' => array(
'route' => '/application/support/listeEmplacementsSupport[/:pkSupport]',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Support',
'action' => 'listeEmplacementsSupport',
),
),
),
'application' => array(
'type' => 'Literal',
'options' => array(
'route' => '/application',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action[/:id][/:dr]]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
),
),
我使用以下命令在视图中构建一个链接
<a href="/application/products/productsList/mandan/<?php echo $coreg['idProduct']; ?>" >
<img height='20' src="/img/mail-recevoir-32.png" alt='products' />
</a>
'router' => array(
'routes' => array(
'application' => array(
// .. same
'child_routes' => array(
'default' => array(
// .. Same
),
),
),
'Products' => array(
// ..same
),
'productsList' => array(
// ..same
),
'Emplacement' => array(
// ..config
),
),
),
路线按顺序匹配
:
通常,应该按照后进先出的顺序查询路由,因此名称RouteStack背后的原因也是如此。[…]您需要确保注册可能重叠的路由,以便最特定的匹配首先匹配(即,稍后注册)
因此,根据您当前的配置,由于所有路由都是在顶层注册的,因此在给产品列表
机会之前,将先检查最后一个路由(应用程序/默认值
),然后再进行匹配
另外,由于application/default
定义了一个可选的id
参数作为第一个参数,这也是使用$this->params('id')
时获得类型
参数值的原因
Soultion
最简单的解决办法就是重新排列路线;将应用程序
路由向右移动到定义顶部应该可以工作
顺序如下
<a href="/application/products/productsList/mandan/<?php echo $coreg['idProduct']; ?>" >
<img height='20' src="/img/mail-recevoir-32.png" alt='products' />
</a>
'router' => array(
'routes' => array(
'application' => array(
// .. same
'child_routes' => array(
'default' => array(
// .. Same
),
),
),
'Products' => array(
// ..same
),
'productsList' => array(
// ..same
),
'Emplacement' => array(
// ..config
),
),
),
你也可以给每条路线一个优先权,不管顺序如何,但是我认为让它们保持自然的顺序会更有意义,以避免将来的混乱
当我在这里时
我在评论中提到,您错误地使用了文字
路线,而不是段
。对于产品
路线定义,您也有同样的问题。如果路由是文本的,它将只匹配确切的值(因此您需要在URL中使用/[:id]
)
因此,您还需要将此路线更新为段
,以便将产品id视为参数
最后,有一种在视图中构造URL的更简单的方法,使用。您的视图脚本代码将更改为
<?php
$type = 'mandan';
$id = $coreg['idProduct'];
?>
<a href="<?php echo $this->url('productsList', compact('type', 'id')); ?>">
<img height='20' src="/img/mail-recevoir-32.png" alt='products' />
</a>
和$param=$this->params('type')代码>不起作用?不,它不起作用,当我做$this->params('id')时,我得到类型param的值,而id的值我不知道…您的路由被定义为文本
路由,这真的应该是段
。你确定这条路线就是被击中的路线吗?如果执行了控制器操作,则可以通过回显$this->getEvent()->getRouteMatch()->getMatchedRouteName()进行检查代码>从控制器操作中。此外,它不会解决您的问题,在视图中构造链接的标准(也是更方便的方法)是通过该类型的“我确实尝试过”,但它没有解决问题。