Hi!
I want to thank the Yii community, to help me learning this amazing framework, by contribute with a GPT extension, super easy to use. Today was my day, and I made my first demo extension.
But have to clear some misunderstanding. My scenario is like this in my gptagent7
extension:
I have a controller rdng\gptagent7\controllers\ChatController
and a widget rdng\gptagent7\widgets\ChatBot
. In my ChatBot widget I got this piece of code:
// Button
$return .= Html::a('test', ['chat/chat']);
$return .= Html::button($this->buttonTitle, ['onclick' => "
let question = $('#chatInput').val();
$.ajax({
type :'post',
cache : false,
data : {
question: question,
},
url : '".Url::to(['/chat/chat'])."',
success : function(response) {
$('#messages').html(response);
},
error : function(response) {
alert(response);
}
});
return false;
", 'class' => $this->buttonClass]);
Of course the ChatBot lives inside a Yii2 app.
I try the standard approach Iβm used to when doing ajax calls i.e. Url::to('/chat/chat')
, but this calls the βmainβ appβs controller.
What I must do, to call in my widget the ChatWidget the actionChat()?
I found some example, but I donβt know if this is correct approach, thus I donβt know how to replace Url::to(β¦) with this request:
use yii\helpers\Url;
use yii\httpclient\Client;
// ...
// Generate the URL for the extension's controller action
$url = Url::to(['/my-extension/my-action']); // Adjust the route accordingly
// Create an HTTP client instance
$client = new Client();
// Make a POST request to the extension's controller action
$response = $client->createRequest()
->setMethod('POST')
->setUrl($url)
->send();
// Check the response
if ($response->isOk) {
// Action was successfully called
$content = $response->content;
// Further processing...
} else {
// Error handling
echo 'Error: ' . $response->statusCode;
}
If there is a simpler way to do it, please help me.
Thanks and have a nice day!