zend-framework – 使用Zend_Form登录的典型问题
Iam面临着使用Zend框架提交表单的典型问题.基本上我已经编写了一个简单的代码来登录用户,但这个问题突然出现了.
显示表单的代码非常标准 $loginform = new Application_Form_Login(); 当我使用我的主页网址为 – http://localhost.ruin.com/public/时 我得到一个例外 Page not found Exception information: Message: Invalid controller specified (login) Stack trace: #0 C:domainsruinlibraryZendControllerFront.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http),Object(Zend_Controller_Response_Http)) #1 C:domainsruinlibraryZendApplicationBootstrapBootstrap.php(97): Zend_Controller_Front->dispatch() #2 C:domainsruinlibraryZendApplication.php(366): Zend_Application_Bootstrap_Bootstrap->run() #3 C:domainsruinpublicindex.php(27): Zend_Application->run() #4 {main} Request Parameters: array ( 'controller' => 'login','action' => 'index','module' => 'default','username' => 'fsdf','password' => 'fdsf','submit' => 'submit',) 但是,如果我使用基本URL作为http://localhost.ruin.com/public/index/,相同的代码完美地工作. 我也知道它的原因在于,在第一个url中,zend路由器正在通过登录搞乱索引控制器,因为它无法将登录操作附加到默认索引控制器. 你们认为这是Zend Framework的设计吗?我必须强行将我的用户发送到这个网址 有什么建议?
简短回答:
$form->setAction('/public/index/login'); 可笑的回答如下:;-) 混淆的一点是使用“行动”一词. 关于表单,“action”指的是action属性: <form action="/url/at/which/the/form/will/be/processed" method="post"> 这是您调用$form-> setAction()方法时引用的操作.关键点在于它必须是一个URL,并且应用程序必须具有将此URL映射到(控制器,操作)对的路由. 这引出了使用术语“动作”的另一种方式:作为控制器上方法的简写名称.例如,名为“smile”的操作映射到控制器上的方法smileAction(). 因此,在您的情况下,问题是让您的表单的setAction()调用与应用程序的路由同步. 通过将URL“login”指定为表单的操作,您将提供相对URL,因此浏览器会将其解释为相对于浏览器位置栏中显示的URL.当您浏览到该页面但没有关闭URL的“索引”部分时,框架中的默认路由会将“login”视为控制器.由于您没有LoginController,因此请求会崩溃. 所以你的IndexController看起来像: <?php class IndexController extends Zend_Controller_Action { public function indexAction() { $this->view->form = $this->_getForm(); } public function loginAction() { $form = $this->_getForm(); if ($this->getRequest()->isPost()){ if ($form->isValid($this->getRequest()->getPost())){ // All cool. Process your form,// probably with a redirect afterwords to // clear the POST. } } // Still alive? // Then it was either not a post request or the form was invalid. // In either case,set the form in the view $this->view->form = $form; } /** * A helper method to keep the form creation DRY */ protected function _getForm() { $loginform = new Application_Form_Login(); $loginform->setMethod('post'); // Points the form to the IndexController::loginAction(); $loginform->setAction('/public/index/login'); return $loginform; } } 结果是setAction()调用需要一个URL,路由器可以映射到控制器/动作对,知道如何处理帖子. 希望这可以帮助! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |