user006779
user006779

Reputation: 1031

how to run action of frontend controller from backend in yii2

Is it possible to run action of frontend controller from backend?

this code works if called action is in backend too. can i specify in runAction that controller/action is in frontend?

Yii::$app->runAction('controller/action')

Also i' m tried something like

$c=new controller();
$s->action();

too but it seems it not working too. //new controller() need some parameters and i have no idea what it is.

Upvotes: 0

Views: 2198

Answers (2)

Cap
Cap

Reputation: 61

The yii\base\Application object has a public property controllerNamespace, which defaults to app\\controllers. You need to change it accordingly to changing default controller namespace.

Change namespace in action:

Yii::$app->controllerNamespace = 'frontend\controllers' and use runAction

Upvotes: 1

ScaisEdge
ScaisEdge

Reputation: 133360

A way could be this .

In your backend application config you could create an additional 'UrlManager' component name eg: urlManagerFrontEnd

  return [
      'components' => [
          'urlManager' => [
              // here is your backend URL rules
          ],
          'urlManagerFrontEnd' => [
              'class' => 'yii\web\urlManager',
              'baseUrl' => 'http://your_path/frontend/web/index.php',
              'enablePrettyUrl' => true,
              'showScriptName' => false,
          ],

      ],
  ];

Then you should invoke following to compose front-end URL:

    Yii::$app->urlManagerFrontEnd->createUrl();

and add the controller/action you prefer

remeber that

runAction()   

Runs an action within this controller with the specified action ID and parameters.

http://www.yiiframework.com/doc-2.0/yii-base-controller.html#runAction()-detail

this mean that cannot run an action of another controller or of another application ..

If you need a service then you must configure a RESTFull or you simply need a redirection you can use redirect

Upvotes: 0

Related Questions