CloudSeph
CloudSeph

Reputation: 883

run yii action controller from another controller in different file

Let's say I have a controller called runAction() in file/test/backend/TestController, but I want to run the action runAction2 in file/test/frontend/TestController2 (i want to run runAction2() in runAction()). How can i do so in yii2?

Upvotes: 0

Views: 247

Answers (3)

Nikola
Nikola

Reputation: 493

The proper way to run another action is to use runAction method of the application

Yii::$app->runAction('controller/action', ['param'=>'value']);

Upvotes: 0

CloudSeph
CloudSeph

Reputation: 883

I have found the solution. All I have to do is put the following code at the top of backend/TestController file.

use frontend/TestController2;

and to use a specific action in that controller i have to add the following in backend/TestController file:

runAction()
{
TestController2::runAction2()
}

Upvotes: 0

jithin
jithin

Reputation: 920

in your

frontend/config/main.php

add URL manager for access backend.

'components' => [
 'urlManagerbackend' => [
                'class' => 'yii\web\urlManager',
                'baseUrl' => '/test/backend/',
                'enablePrettyUrl' => true,
                'showScriptName' => false,
  ],
]

after this create URL to action like below:

Yii::$app->urlManagerbackend->createUrl(['test/runAction2']);

Upvotes: 2

Related Questions