将字符串/文本发送到cakePHP中的方法/函数

时间:2012-08-29 09:09:13

标签: cakephp formhelper

大家好日子。我目前正在使用cakePHP开发聊天应用程序。它将是一个专注于回答问题的聊天应用程序。这意味着用户将根据他/她的问题收到自动回复。我正在使用聊天界面,不需要用户登录。一旦用户发出问题,聊天应用程序将仅与数据库表交互。现在我的问题是如何将问题发送到控制器中将被解析的方法。我试图在视图文件中执行以下操作:

<!--View/People/index.ctp-->
<h1>This is the chat interface</h1>
<?php $this->Html->charset(); ?>

<p>
<!--This is the text area where the response will be shown-->
<?php
echo $this->Form->create(null);
echo $this->Form->textarea('responseArea', array('readonly' => true, 'placeholder' => 
'***********************************************************************************
WELCOME! I am SANTI. I will be the one to answer your questions regarding the enrollment process 
and other information related to it. ***********************************************************************************', 'class' => 'appRespArea'));
echo $this->Form->end();
?>
</p>

<p>
<!--This is the text area where the user will type his/her question-->
<?php 
echo $this->Form->create(null, array('type' => 'get', 'controller' => 'people', 'action' => 'send', ));
echo $this->Form->textarea('userArea', array('placeholder' => 'Please type your question here', 'class' => 'userTextArea'));
echo $this->Form->end('Send');
?>
</p>

这是控制器:

<!--Controller/PeopleController.php-->
<?php
class PeopleController extends AppController{
    public $helpers = array('Form');

    public function index(){

    }

    public function send(){
        //parsing logic goes here
    }
}
?>

正如您所看到的,我告诉index.ctp中的表单将操作指向PeopleController中的send()方法,以便它可以在与数据库交互之前解析问题。单击按钮时出现的问题是我总是被重定向到/ users / login,这不是我想要发生的事情。我只是希望应用程序指向/ people / send。在那种情况下似乎是什么问题?我试图在互联网和文档中寻找答案然后测试它们,但到目前为止还没有解决问题。有人可以帮我这个吗?我这么多天都试图解决这个问题。

我继续收到此错误:

Missing Method in UsersController
Error: The action *login* is not defined in controller *UsersController*

Error: Create *UsersController::login()* in file: app\Controller\UsersController.php.

<?php
class UsersController extends AppController {


public function login() {

}

}

1 个答案:

答案 0 :(得分:1)

如果您使用的是Auth Component,则可能需要更改PeopleController代码:

<!--Controller/PeopleController.php-->
<?php
class PeopleController extends AppController{
    public $helpers = array('Form');

   public beforeFilter()
   {
      parent:: beforeFilter();
      $this->Auth->allow('index', 'send');
   }

   public function index(){

   }

   public function send(){
    //parsing logic goes here
   }
}
?>

这是因为您使用people / send作为表单操作。并且用户未登录,这意味着没有设置任何Auth会话。这就是为什么它总是将用户重定向到登录页面,如果没有登录页面,那么它会显示错误。

所以我也将send()方法设为公开,这样任何人都可以访问它。 希望这个概念对你有所帮助。