Zend framework2 在URL中传递数据时的路由
我已经使用框架应用程序和模块创建了一个基本的Zend Framework 2应用程序,尽管我遇到了一个路由问题,似乎无法解决它 在我的模块中,它松散地基于Zend Framework网站上新的Quickstart模块,我在module.config.php中有以下路径Zend framework2 在URL中传递数据时的路由,zend-framework2,Zend Framework2,我已经使用框架应用程序和模块创建了一个基本的Zend Framework 2应用程序,尽管我遇到了一个路由问题,似乎无法解决它 在我的模块中,它松散地基于Zend Framework网站上新的Quickstart模块,我在module.config.php中有以下路径 'router' => array( 'routes' => array( 'blog' => array( 'type' => 'Literal',
'router' => array(
'routes' => array(
'blog' => array(
'type' => 'Literal',
'options' => array(
// Change this to something specific to your module
'route' => '/blog',
'defaults' => array(
// Change this value to reflect the namespace in which
// the controllers for your module are found
'__NAMESPACE__' => 'Blog\Controller',
'controller' => 'View',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
// This route is a sane default when developing a module;
// as you solidify the routes for your module, however,
// you may want to remove it and replace it with more
// specific routes.
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '[/:controller[/:action]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
),
),
使用标准模块、控制器和操作URL请求时,此路由似乎工作正常:
blah.com/blog/post/view
我遇到的问题是当我试图通过URL传递一些数据时:
blah.com/blog/post/view/id/1
两个URL中的后一个导致404。我猜我需要一个子路由来允许在URL上传递数据,尽管我一直坚持到底需要什么。有人能给我指出正确的方向吗?我已经浏览了参考指南,尽管我似乎还没有越过这条线
非常感谢您的帮助。您必须在路线中添加可选路段。还要为其添加默认值:尝试以下操作:
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '[/:controller[/:action[/id[/:idvar]]]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'idvar' => '0'
),
),
),
在控制器操作中,请尝试:
$id=$this->params('idvar')
要检索
idvar
值。您必须将可选段添加到路线中。还要为其添加默认值:尝试以下操作:
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '[/:controller[/:action[/id[/:idvar]]]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'idvar' => '0'
),
),
),
在控制器操作中,请尝试:
$id=$this->params('idvar')
要检索
idvar
值。如果您需要多个可选参数,并且可以以任意随机组合提供(例如pageNumber=2,participation=active),则需要添加查询类型的子路由,这样您可以提供完全灵活的GET参数:
可以使用此语法使用url视图帮助器构建适合以下示例的url:
$this->url('event/index/query',array('o_id'=>1,'participation'=>'active','pageNumber'=>'2')
如果您需要多个可选参数,并且可以以任意随机组合提供(例如pageNumber=2,participation=active),则需要添加查询类型的子路由,这样您可以提供完全灵活的GET参数: 可以使用此语法使用url视图帮助器构建适合以下示例的url: $this->url('event/index/query',array('o_id'=>1,'participation'=>'active','pageNumber'=>'2')