Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 Override generic create Action in Rest ActiveController

Tags:

rest

yii2

I have a Rest API in Yii2, and Yii generates all actions

view / update / create / delete

I want to change the comportement of createMethode et garde the other methods, SO I can't use the class Controller, I should use the class ActiveController

But I want that class do the same work, I need to add some action before creating and some actions after creating. So I need to override the actionCreate

how can I do it ?

like image 850
Touhami Avatar asked Dec 25 '14 15:12

Touhami


2 Answers

You can do the following

class CountryController extends ActiveController
{
    public $modelClass = 'common\models\Country';

    public function actions()
    {
        $actions = parent::actions();
        unset($actions['create']);
        return $actions;
    }

    public function actionCreate(){
        // implement here your code

    }

}
like image 112
Roman Podlinov Avatar answered Nov 01 '22 02:11

Roman Podlinov


I know this is an old question; but I found it in a related search and thought I could provide a nice, updated solution and summary of what is here now. Hopefully someone finds this helpful...

I know of two ways to do this. You can define an action method, like @Roman Podlinov mentioned, or you can define the action as a separate class and point the default create action to it, close to what @mrJ0ul3 mentioned. I'll provide examples of both below for the sake of completeness.

Method 1 - action method in controller

In the controller, override the actions method like so:

public function actions()
{
    $actions = parent::actions();
    unset($actions['create']);
    return $actions;
}

And then you can define your create method in that controller.

public function actionCreate()
{
    // your code goes here
}

And to be clear, the method name does need to be actionCreate as shown above. I saw a comment mentioning createAction, but that is incorrect. They may be getting confused with the CreateAction class which is a part of the yii\rest namespace and holds the code that actually runs with the default REST create action. However, if you use this method to override the create request logic, then that class doesn't really matter. It does matter though for the other method...

Method 2 - separate action class

Personally, I prefer and use this method to override the default actions. I like keeping the default CRUD action logic out of the controller itself. It makes it nice to know that if there are action methods in a controller of mine that they are completely custom actions. This is definitely just personal preference though.

For this, you will create a new class file that should contain code that looks something like this:

namespace app\actions\controllerName

class CreateAction extends \yii\rest\CreateAction
{
    public function run()
    {
        // your code goes here
    }
}

If you go this route, I'd recommend checking out the body of Yii's REST CreateAction class and model the progression of your code after their's, assuming it makes sense to do so for your use case.

Now you just need to tell the controller to use this action class for the create action. You do that in the actions method of the controller like this:

public function actions()
{
    $actions = parent::actions();
    $actions['create']['class'] = 'app\actions\controllerName\CreateAction';
    return $actions;
}

As you can see, it is similar in syntax to what @mrJ0ul3 was suggesting; but that suggestion only changed how one part of one action would work. It doesn't allow you to fully override the logic of the action - which is why it missed the mark at answering the actual question.

Hope that helps!

-Cheers everyone! :)

like image 33
darksnake747 Avatar answered Nov 01 '22 03:11

darksnake747