如何使用 Zend Framework 2 按 url 传递参数
How to pass parameters by url with Zend Framework 2
我在 Zend Framework 2 中通过 url 传递参数时遇到了麻烦。例如:我想通过这样的 URL 访问页面:
http://example.com/student/details/aaa 它在工作,但是如果我的 url 是这样的:
http://example.com/student/details/1 它不起作用,我仍然收到 404。
我尝试按照 how to pass params using a route in Zend Framework 2?
的说明进行操作
但是还是不行
这是我的 module.config.php
'student' => array(
'type' => 'segment',
'options' => array(
'route' => '/student[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'__NAMESPACE__' => 'AppFeeder\Controller',
'controller' => 'Student',
'action' => 'index',
),
),
),
我在我的控制器中使用这个$this->getEvent()->getRouteMatch()->getParam('id')
。
谁能帮帮我?
您对 id 使用了错误的路由规则,该规则表示从字母表开始然后是字母表+数字+“_-”。
'id' => '[a-zA-Z][a-zA-Z0-9_-]*',
相反,您需要更改从字母表+数字开始然后字母表+数字+“_-”的规则
'id' => '[a-zA-Z0-9][a-zA-Z0-9_-]*',
我在 Zend Framework 2 中通过 url 传递参数时遇到了麻烦。例如:我想通过这样的 URL 访问页面: http://example.com/student/details/aaa 它在工作,但是如果我的 url 是这样的: http://example.com/student/details/1 它不起作用,我仍然收到 404。
我尝试按照 how to pass params using a route in Zend Framework 2?
的说明进行操作但是还是不行
这是我的 module.config.php
'student' => array(
'type' => 'segment',
'options' => array(
'route' => '/student[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'__NAMESPACE__' => 'AppFeeder\Controller',
'controller' => 'Student',
'action' => 'index',
),
),
),
我在我的控制器中使用这个$this->getEvent()->getRouteMatch()->getParam('id')
。
谁能帮帮我?
您对 id 使用了错误的路由规则,该规则表示从字母表开始然后是字母表+数字+“_-”。
'id' => '[a-zA-Z][a-zA-Z0-9_-]*',
相反,您需要更改从字母表+数字开始然后字母表+数字+“_-”的规则
'id' => '[a-zA-Z0-9][a-zA-Z0-9_-]*',