代码之家  ›  专栏  ›  技术社区  ›  Jairo

向cakeHP中的方法/函数发送字符串/文本

  •  0
  • Jairo  · 技术社区  · 12 年前

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

    <!--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,这不是我想要的。我只想让应用程序指向/peoples/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 回复  |  直到 12 年前
        1
  •  1
  •   Arun Jain    12 年前

    如果您使用的是Auth组件,那么您可能需要更改 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作为表单操作。并且用户没有登录,这意味着没有设置任何身份验证会话。这就是为什么它总是将用户重定向到登录页面,如果没有登录页面,它会向您显示错误。

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