wolvorinePk
wolvorinePk

Reputation: 1790

yii2 creating custom based Rest API

I am creating my own custom based action for some calculation, which has to be send by restful api in json. The problem is when i create the controller inherited from activecontroller, then for the index action it asks a model class.

I must need to use index action with my own custom based calculation which does not need to be store in the database so i actually do not need to create model class at all. This is first time i am using activecontroller so i am not sure how to make the index page act as according to my needs.

I am able to create my own custom based action with any name i want e.g.

 public function actionFast()
    {
        echo "XXX";
    }

and inside the config/ web.php

'urlManager' => [
            'showScriptName' => false,
            'enablePrettyUrl' => true
            ,
            'rules' => [
                ['class' => 'yii\rest\UrlRule', 'controller' => ['Apprest/Fast'], 'extraPatterns' => ['GET dast' => 'fast']],
            ],
        ],

but instead of my custom "Fast" action i need to use index action for all kinds of calculations. Is there a way to do that?

Secondly i want to make this url restricted for GET only url , is there a way to make send the user to 404 error incase he tries to post on this page?

Upvotes: 1

Views: 2901

Answers (1)

Mikola laviro
Mikola laviro

Reputation: 46

If you don't have model, you can extends right from yii\rest\Controller and add actions that you need

Here you can see example:

namespace api\controllers;
use yii\rest\Controller;
class MyController extends Controller
{
public function actionFast()
    {
        echo "XXX";
    }
}

Upvotes: 0

Related Questions