PHP   发布时间:2022-04-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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',)

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

我也知道它的原因在于,在第一个url中,zend路由器正在通过登录搞乱索引控制器,因为它无法将登录操作附加到认索引控制器.

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

有什么建议?

简短回答:
$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,路由器可以映射到控制器/动作对,知道如何处理帖子.

希望这可以帮助!

大佬总结

以上是大佬教程为你收集整理的zend-framework – 使用Zend_Form登录的典型问题全部内容,希望文章能够帮你解决zend-framework – 使用Zend_Form登录的典型问题所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: