Routing ZF2通过post VAR路由
我希望通过处理http post VAR来路由请求,例如,将“模块”、“控制器”、“操作”等放在表单中,将表单的action=“…”目标设置为应用程序的默认路由,并从那里路由到模块/控制器/操作路由。 URL/module/controller/action不能访问到module/controller/action的路由,因此问题是,如果在module.config.php中配置了路由,那么它们也可以通过URL访问? 如果我知道module.config.php中路由的正确配置,或者有必要设置自己的自定义路由服务,那么我可能会遗漏一点,这可能是简单明了的 重定向到路由将在浏览器的地址栏中显示路由URL,这是我想要避免的,我不知道如何避免Routing ZF2通过post VAR路由,routing,zend-framework2,Routing,Zend Framework2,我希望通过处理http post VAR来路由请求,例如,将“模块”、“控制器”、“操作”等放在表单中,将表单的action=“…”目标设置为应用程序的默认路由,并从那里路由到模块/控制器/操作路由。 URL/module/controller/action不能访问到module/controller/action的路由,因此问题是,如果在module.config.php中配置了路由,那么它们也可以通过URL访问? 如果我知道module.config.php中路由的正确配置,或者有必要设置自
if ($this->request->isPost()) {
$post = $this->request->getPost();
if (isset($post->module) && isset($post->controller) && isset($post->action)) {
return $this->redirect()->toRoute($post->module, array(
'controller' => $post->controller,
'action' => $post->action
));
}
}
编辑:找到半个解决方案:
使用\Mvc\Controller\Plugin\Forward,如下所示:
在应用程序的默认控制器/操作中,按照module.config.php中的invokables部分中定义的名称调用控制器,例如,对于名为“register”的控制器:
if ($this->request->isPost()) {
$post = $this->request->getPost();
if (isset($post->module) && isset($post->controller) && isset($post->action)) {
return $this->forward()->dispatch('register', array('action' => $post->action));
}
}
现在我需要从模块/控制器名称中获取控制器可调用名称,就像从post变量中获取一样。如何?解决方案的关键是在模块的module.config.php中定义唯一的控制器可调用名称,使控制器可调用名称能够以结构化定义的方式构造,例如,对可调用名称使用语法“MyModule\controller\MyController” 示例应用程序: 模块/控制器:
- 应用
- 索引
- 使用者
- 索引
- 登录
- 登记册
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Hostname',
'options' => array(
'route' => '4yougroup.local.f4u.0101',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
),
),
),
文件module/User/config/module.config.php中的代码:
(注意:此处未定义任何路线!)
为了使用HTTP POST VAR通过应用程序的默认路由对所有内容进行路由,请将以下代码放入应用程序模块的IndexController indexAction函数中:
class IndexController extends AbstractActionController
{
public function indexAction()
{
if ($this->request->isPost()) {
$post = $this->request->getPost();
if (isset($post->module) && isset($post->controller) && isset($post->action)) {
$controllerName = ucfirst($post->module) . "\\Controller\\" . ucfirst($post->controller);
return $this->forward()->dispatch($controllerName, array('action' => $post->action));
}
}
}
}
然后在视图中放置如下HTML代码:
<form class="form-horizontal" action="" method="POST">
<input type="hidden" name="module" value="user">
<input type="hidden" name="controller" value="register">
<input type="hidden" name="action" value="index">
……瞧
如果有人能提出更精简、更清洁的解决方案,那就好了:)
class IndexController extends AbstractActionController
{
public function indexAction()
{
if ($this->request->isPost()) {
$post = $this->request->getPost();
if (isset($post->module) && isset($post->controller) && isset($post->action)) {
$controllerName = ucfirst($post->module) . "\\Controller\\" . ucfirst($post->controller);
return $this->forward()->dispatch($controllerName, array('action' => $post->action));
}
}
}
}
<form class="form-horizontal" action="" method="POST">
<input type="hidden" name="module" value="user">
<input type="hidden" name="controller" value="register">
<input type="hidden" name="action" value="index">