Zend framework2 MVC路由和url视图帮助程序
我有一个关于MVC路由的问题 我在module.config.php中设置了如下路由:Zend framework2 MVC路由和url视图帮助程序,zend-framework2,Zend Framework2,我有一个关于MVC路由的问题 我在module.config.php中设置了如下路由: 'router' => array( 'routes' => array( 'album' => array( 'type' => 'segment', 'options' => array( 'route' => '/album[/page/:page]', 'constr
'router' => array(
'routes' => array(
'album' => array(
'type' => 'segment',
'options' => array(
'route' => '/album[/page/:page]',
'constraints' => array(
'page' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Trade\Controller\Album',
'action' => 'index',
'page' => 1,
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'segment',
'options' => array(
'route' => '[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
),
),
),
),
有以下路线:
/album
/album/page/2
/album/edit/3
/album/add
在视图脚本中,我使用:
<a href="<?php echo $this->url('album/default', array('action'=>'edit', 'id' => $album->id));?>">Edit</a>
这不是我想要的url的样子。我希望url看起来像
/专辑/编辑/3
由于我没有在url参数数组中提供页码,所以我不希望获取默认页面
我相信有一个更聪明的方法来设置所需的路由,并将感谢任何指针
Peter在module.config.php中更改路径,如下所示
'router' => array(
'routes' => array(
'album' => array(
'type' => 'Literal',
'options' => array(
'route' => '/album',
'defaults' => array(
'controller' => 'Trade\Controller\Album',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'add' => array(
'type' => 'Literal',
'options' => array(
'route' => '/add',
'defaults' => array(
'action' => 'addalbum',
),
),
),
'edit' => array(
'type' => 'Segment',
'options' => array(
'route' => '/edit[/:id]',
'constraints' => array(
'id' => '[0-9]+',
),
'defaults' => array(
'action' => 'editalbum',
),
),
),
'page' => array(
'type' => 'Segment',
'options' => array(
'route' => '/page[/:id]',
'constraints' => array(
'id' => '[0-9]+',
),
'defaults' => array(
'action' => 'pagealbum',
),
),
),
),
),
),
),
现在将视图脚本中的链接更改为
<a href="<?php echo $this->url('album/edit', array('id'=>$album->id)); ?>">Edit</a>
您的路线现在就可以了。您是否已经尝试过使用
'page'=>null
?是的,我尝试过,但没有成功。谢谢!这很有效,是一个很好的解决方案和实践,因为已经定义了确切的路线!
<a href="<?php echo $this->url('album/edit', array('id'=>$album->id)); ?>">Edit</a>