0

I have a yii2 advanced template project and has 4 main modules.backend,frontend,api and common.

I have recently created a component in common which has 2 3 function in it.Main method is sendMessage which uses another method in same class which is findModel($id).

When i access this component from backend i can access that component with following line

Yii::$app->myComponent->sendMessage()

but i cant use the same code in api module.

Is there a specific reason for that or is there another way to access all those component methods in api module from common component.

I have created a restful api in that api folder.

2
  • 1
    have you defined your components in common/config/main.php or backend/config/main.php? And does your api-config merge with common-config? Commented Jun 23, 2016 at 9:42
  • @Jørgen I have defined in common/config/main.php but i dont know how to merge api-config with common-config. I have added api in common/config/aliases.php. Can you give me an example of how to merge? Thank you Commented Jun 23, 2016 at 11:28

1 Answer 1

0

You would have to merge your config file with the components settings into your api config. In the request lifecycle you can see that the configurations are set before the application is initiated. So in your case your api/web/index.php would look something like your backend/web/index.php:

<?php
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');

require(__DIR__ . '/../../vendor/autoload.php');
require(__DIR__ . '/../../vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/../../common/config/bootstrap.php');
require(__DIR__ . '/../config/bootstrap.php');

$config = yii\helpers\ArrayHelper::merge(
   require(__DIR__ . '/../../common/config/main.php'),
   require(__DIR__ . '/../../common/config/main-local.php'),
   require(__DIR__ . '/../config/main.php'),
   require(__DIR__ . '/../config/main-local.php')
);

$application = new yii\web\Application($config);
$application->run();

Remember that this is environmentally different depending on weather you're in dev or prod.

Sign up to request clarification or add additional context in comments.

1 Comment

yes i do have that same file in all folders backend/web,frontend/web and api/web

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.