0

I have module created in the basic project of yii2 and now i want to access or use that module another project/application of mine....

How can I achieve this.

please help me out here.

  • You might want to make [an extension](https://www.yiiframework.com/wiki/809/how-to-create-yii2-extension-and-push-to-github-and-register-into-packagist-to-allow-end-user-install-from-composer) – M. Eriksson Feb 17 '22 at 07:47

1 Answers1

1

To use module in different apps there are 3 things you need.

  1. The module must not be dependent on classes from core project. For any class that needs to be implemented by core project the module should define interface and depend on that interface instead of class itself.
  2. The module should use different namespace than app and autoloader must know how to load classes from that namespace. (more about that later)
  3. You have to add module in your config in same way you've added it in first project.

The points 1 and 3 are pretty much self-explaining. If are not sure how to add module in config see the yii2 guide.

Now back to the second point. While naive way of copying module over to second project would work it will turn maintaining the module into nightmare because each change would have to be done in each copy of module. So it's better to keep the code of module in one place and make it available for each project. There are multiple ways of doing that.

If you want to, you can turn your module into extension and make it publicly available through packagist as it was suggested by M. Eriksson in comments. After that you would simply add your extension through composer as any other dependency.

Composer also allows you to define and use private repositories if you don't want to publish your module at packagist. See composer documentation for more details.

The most trivial way is to simply put the code into separate folder outside of project. If you do that, you have to make sure that autoloaders in your projects are capable of finding the files locations to load classes. There are two options how to do that. In any case you will want to avoid conflicts with namespaces used by your project, that's why you need to use different namespace.

Let's assume that you've put your module files into folder /path/to/modules/myModule and all classes in your module belongs to namespace modules\myModule. You have to make sure that your webserver can access that folder and that it can run php scripts there.

First option is to use Yii's autoloader. That autoloader uses aliases to look for classes. If you add @modules alias and point it to /path/to/modules folder, the Yii autoloader will try to look for any class from modules\* namespace in /path/to/modules folder. You can add the alias in your config file (web.php, console.php or any other config file you use):

return [
    // ...
    'aliases' => [
        '@modules' => '/path/to/modules',
        // ... other aliases ...
    ],
];

The second option is to use project's composer.json file to set autoloader generated by composer to load your classes.

{
    "autoload": {
        "psr-4": {
            "modules\\": "/path/to/modules"
        }
    }

}

You can find more info about this in composer's documentation. Don't forget to run composer dump-autoload after you change autoload settings in your composer.json file to update the generated autoloader.

Michal Hynčica
  • 5,038
  • 1
  • 12
  • 24