Search code examples
cakephpformhelper

Sending string/text to a method/function in cakePHP


Good Day to all. I am currently developing a chat application using cakePHP. It will be a chat application that focuses on answering questions. That means the user will receive an automated response based on his/her question. I am working on the chat interface right now that doesn't require the user to login. The chat application will only be interacting to a database table once the user has sent the question. Now my problem is on how to send the question to a method in the controller where it will be parsed. I tried to do the following in the view file:

<!--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>

This is the controller:

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

    public function index(){

    }

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

As you can see, I am telling the form in index.ctp to point the action to the send() method in PeopleController so it can parse the question before interacting with the database. The problem that arises when I click the button is that I am always redirected to /users/login which is not what I want to happen. I just want the application to point itself to /people/send. What seems to be the problem in that case? I have tried to look for answers both in the Internet and in the documentation and then tested them but nothing has resolved the problem so far. Can anyone please help me on this? I've been trying to resolve this for so many days.

I keep on getting this error:

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() {

}

}

Solution

  • If you are using Auth Component, then you might need to change your PeopleController code:

    <!--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
       }
    }
    ?>
    

    This is because of you used people/send as a form action. And the user is not logged in, it means there is no any Auth session has been set. Thats why it always redirect the user to login page, and if there is not login page, then it will show you the error.

    So I made the send() method also public, so that anyone can access it. Hope this concept will help you.