4

How I can load an external-site module? I have a common module I need to load in distinct Yii2 sites, like advanced-template my idea is to have a common dir where store generic modules which I can load to each site. A file system structure can be like this:

/
  site-1/
    (loads modules from common-modules dir for site-1)
  site-2/
    (loads modules from common-modules dir for site-2)
  common_sites_modules/
    module-1/
    module-2/
    carrello/
        Carrello.php

Each site in his configuration have to load modules from common-modules/ Is possible to implement this structure?

Edit 1

The configuration:

'cart' => [
    'class' => dirname(dirname(dirname(__DIR__))) . '/common_sites_modules/carrello/Carrello',
    'params' =>[
        ...
    ],
    'components' => [
        ...
    ],
],

and this is the first line of the class Carrello.php:

<?php
namespace common_sites_modules\carrello;
...

The top bar of editor with the path of the class and the error returned by Yii: Sublime top bar

enter image description here

Edit 2:

Thanks to @Yupik for support and suggests, the new settings:

bootstrap.php:

Yii::setAlias('@common-modules', dirname(dirname(dirname(__DIR__))) . '/common_sites_modules');

main-local.php:

'class' => '@common-modules\carrello\Carrello',

The generated error:

enter image description here

Like suggested in the comments the solution is to declare an alias and then use the name of alias for call the module. Like suggested by @Yupik I've set in the common/config/bootstrap.php an alias as follow:

Yii::setAlias('@common_modules', dirname(dirname(dirname(__DIR__))) . '/common_modules');

In the main configuration:

'carrello' => [
    'class' => ''common_modules\carrello\Carrello',
     ...
]

Obviously namespace have to be configured based on the position on filesystem. Thanks for the suggestions

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
MarBer
  • 535
  • 1
  • 5
  • 22
  • Yes, it is possible. – Yupik Jul 18 '17 at 16:59
  • Ok but I have some problem to load module, in the `class` parameter of configuration i set the absolute path of module but every time i receive the `Class /var/www/html/common_sites_modules/my-module/MyClass does not exist` exception when new ReflectionClass object was generated with the path, the path is correct so why this exception? – MarBer Jul 19 '17 at 06:09
  • Propably because youre doing something wrong. Share the code, otherwise we can't help you. – Yupik Jul 19 '17 at 06:12
  • Do you have any ideas about this? – MarBer Jul 19 '17 at 06:52
  • 1
    In configuration, don't use `dirname(dirname(....`, just put there `app\component-namespace\MyComponent::class`. Yii class autoloader requires to use path namespacing, so your namespace for your modules/components should be `namespace app\folder_name\carello\;` (or create your own alias and then use it). For this you should read about [Class Autoloading - Yii2](http://www.yiiframework.com/doc-2.0/guide-concept-autoloading.html). You have to adjust this to yii2 style, otherwise it won't be autoloaded. – Yupik Jul 19 '17 at 07:22
  • Ok but the module is out of app namespace, it is in the main server root as other site, the idea is to have a directory to store and share modules and the best way, I think, is to put the modules dir at the same level of yii sites directories but my question is to understand if yii2 can load modules out of app namespace – MarBer Jul 19 '17 at 08:01
  • But you can add custom aliases to your folders - [Yii2 Aliases](http://www.yiiframework.com/doc-2.0/guide-concept-aliases.html) – Yupik Jul 19 '17 at 08:22
  • Ok, with the example configuration of file system in the main question how would you set up the module class parameter? I think this is my main problem – MarBer Jul 19 '17 at 08:37
  • As i said: `'class' => \your_custom_alias\subfolder\carello\Carello::class,` – Yupik Jul 19 '17 at 08:38
  • Well and the alias? – MarBer Jul 19 '17 at 08:39
  • You mean in `config/bootstrap.php`? Like other aliases `Yii::setAlias('your_custom_alias', dirname(dirname(dirname(__DIR__))) . '/my_super_directory');` – Yupik Jul 19 '17 at 08:40
  • Thanks @Yupik for the help, the question is updated with your suggestions but the error persists, maybe some settings are wrong? – MarBer Jul 19 '17 at 08:55
  • You have to change namespace in class aswell: ` \common-modules\carrello\Carrello::class` – Yupik Jul 19 '17 at 08:57
  • Ok now I understand, the error is on the first version of question where the filesystem configuration are an example of the real configuration, on my system the path where Yii doesn't find the class are correct, you can see it in my first question's edit that the error was generated for a path but in my editor the class are opened on the same path wich generate error, for avoid mistake I'm going to edit the first part of question – MarBer Jul 19 '17 at 09:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/149567/discussion-between-marber-and-yupik). – MarBer Jul 19 '17 at 09:10
  • https://yii2-framework.readthedocs.io/en/stable/guide/tutorial-yii-integration/ – SiZE Jul 30 '18 at 05:39

1 Answers1

0

Yes, we can do this by making a symlink of common_sites_modules directory in both site folder (site1, site2).