How To Use Controller In Extension?

I’m making an extension where you can add tags to… whatever you wanna add tags to.

I’ve got the model, view and assets working but I want to add a Controller so get data from the database.

When I made the piece of code this happened via jquery ajax call, but I can’t figure out how to do this the Yii2 way.

Check how Captcha is done. For this purpose you can use Action.

Seems like you may want to create a module and register it as an yii2-extension (as I see, you need a model, view, assets, and controller).

But how can I use a custom controller like the SiteController r=site/search&input=bladiebla when it’s not in the root/fronted/controllers folder ?

You can either make it a module as Kartik V suggested or make it a standalone action as I’ve suggested. Both will require some lines in application config.

If using a module, you can register it in Yii config and call the module related controllers globally. For example:




'modules' = [

    /* yourmod is the name of your module */

    'yourmod => [

           // your namespaced module class location

          'class' => 'vendorname\yourmod\Module',

    ]

];



Then you can refer the module level controllers globally. For example to refer an actionIndex inside UserController inside your yourmod module.




/yourmod/user/index



You can refer an example on how I have done it for a markdown module extension.

Hi,

If you want, you can create a controller in an extension (although it is not necessarily the best practice).

In your conf:




$config = [

...

'controllerMap' => [

        'admin' => 'vendor\myext\controllers\AdminController',

    ],

...

]



my controller AdminController in vendor/myext :




<?php


namespace vendor\myext\controllers;


use Yii;

use yii\web\Controller;




class AdminController extends Controller {


    public function actionIndex (){

        $test = 'admin content';

        return \Yii::$app->view->renderFile ('@app/vendor/myext/views/admin/index.php',['test'=>$test]);

    }


} 



you can put your view files in the classic folder view , creating the "admin" folder in the example ; or by defining a specific view file as in the example.

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.

I have a similar scenario. 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!