使用Zend_Form登录的典型问题

Sum*_*osh 8 zend-framework zend-form

Iam面临着使用Zend框架提交表单的典型问题.基本上我已经编写了一个简单的代码来登录用户,但这个问题突然出现了.

显示表单的代码非常标准

$ loginform = new Application_Form_Login(); $ loginform->使用setMethod( '后'); $ loginform->的setAction( '登录'); $ this-> view-> form = $ loginform;

当我使用我的主页网址时 - http://localhost.ruin.com/public/

我得到一个例外

Page not found
Exception information:

Message: Invalid controller specified (login)
Stack trace:

#0 C:\domains\ruin\library\Zend\Controller\Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#1 C:\domains\ruin\library\Zend\Application\Bootstrap\Bootstrap.php(97): Zend_Controller_Front->dispatch()
#2 C:\domains\ruin\library\Zend\Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#3 C:\domains\ruin\public\index.php(27): Zend_Application->run()
#4 {main}  

Request Parameters:

array (
  'controller' => 'login',
  'action' => 'index',
  'module' => 'default',
  'username' => 'fsdf',
  'password' => 'fdsf',
  'submit' => 'submit',
)  
Run Code Online (Sandbox Code Playgroud)

但是,如果我使用基本URL作为http://localhost.ruin.com/public/index/,相同的代码可以正常工作.

我也知道这是因为在第一个url中,zend路由器正在使用登录来搞乱索引控制器,因为它无法将登录操作附加到默认索引控制器.

你们认为这是Zend Framework的设计吗?每当他们点击主页时,我都必须强行将我的用户发送到这个网址 http://localhost.ruin.com/public/index/或者我有办法让我的代码与 http://localhost.ruin.com/public/一起使用

有什么建议?

Dav*_*aub 2

简短回答:

$form->setAction('/public/index/login');
Run Code Online (Sandbox Code Playgroud)

冗长得可笑的答案如下:;-)

一个令人困惑的地方是“行动”一词的使用。

对于form,“action”指的是 action 属性:

<form action="/url/at/which/the/form/will/be/processed" method="post">
Run Code Online (Sandbox Code Playgroud)

这是调用方法时要参考的动作$form->setAction()。关键点是这必须是一个URL,并且应用程序必须具有将此 URL 映射到(控制器,操作)对的路由。

这就提出了术语“动作”的另一种使用方式:作为控制器上方法的简写名称。例如,名为“smile”的操作映射到smileAction()控制器上的方法。

因此,就您而言,问题之一是使表单的setAction()调用与应用程序的路由同步。

通过将 URL“登录”指定为表单的操作,您将提供相对URL,因此浏览器会将其解释为相对于浏览器地址栏中显示的 URL。当您浏览到该页面但忽略 URL 的“索引”部分时,框架中的默认路由会将“登录”视为控制器。因为你没有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;
    }
}
Run Code Online (Sandbox Code Playgroud)

结果是 setAction() 调用需要一个 URL,路由器可以将其映射到知道如何处理帖子的控制器/操作对。

希望这可以帮助!