Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling action from another controller and returning data Yii2

I have controller where I need to access an action from another controller and return data:

rest/controllers/AController

switch (@$_GET['barcodeType']) 
{
    case '1D':
    {  
            //do action Request1 from BController and return data from 
            this controller
    }
    break;

    case '2D':
    {
            //do action Request2 from BController and return data from 
            this controller
    }
    break;

    default:
    return  ['Wrong barcodeType'];
    break; 
}

soap/controllers/BController

public actionRequest1{
    //do something and return data to AController
}

public actionRequest2{
    //do something and return data to AController
}

How can I do this?

like image 692
PGg Avatar asked Dec 13 '22 18:12

PGg


1 Answers

You can do this, if you need to reuse another controller's action:

$result = Yii::$app->runAction('b/request1', ['param1' => 'value1', /* ... */]);

But I don't recommend it. I suggest you to move the logic to another component, so both controllers can use it.

like image 52
André Sousa Avatar answered Dec 17 '22 22:12

André Sousa