Custom package dependencies classes not found
-
Re: Custom Package dependencies
Hi, I am creating custom package that has composer.json document in itself and uses library:
"stichoza/google-translate-php"
I installed it in package location:
"bagistoProject/packages/myNamespace/myPackage"
with command:
composer require stichoza/google-translate-php
My composer.json inside my package contains:
"require": { "stichoza/google-translate-php": "dev-master" }
and I applied command in root bagistoProject directory:
composer dump-autoload
However, if I try to include it in controller:
... use Stichoza\GoogleTranslate\GoogleTranslate; class MyCustomController extends Controller { public function index() { $translator = new GoogleTranslate(); return view('mypackage::view-location'); } }
I get error:
Class "Stichoza\GoogleTranslate\GoogleTranslate" not found
I can find vendor directory and all classes generated by composer in:
"bagistoProject/packages/myNamespace/myPackage/vendor/stichoza/google-translate-php/src"
I tried to implement the same package in laravel project and it works without problems. What am I missing?
-
Hello @Okumura
Firstly check that your custom package is running or not.
Then check you have followed all the necessary steps regarding the installation of the package installation
https://github.com/bagisto/bagisto-package-generator
Thanks & Regards
-
@Rishabh-Webkul
My custom package is running fine. I did all the steps for creating the package but it doesn't come with composer.json document. I found bagisto article that shows how to correctly set up it by hand:
https://bagisto.com/en/how-to-create-a-package-in-bagisto-v2-0/
but it still doesn't include required libraries in my packages.My current solution is in document:
bagistoProject/packages/MyNamespace/MyPackage/src/Providers/MyPackageServiceProvider.php
and in register method include:
class MyPackageServiceProvider extends ServiceProvider { public function register() { $packageVendorPath = __DIR__ . '/../../vendor/autoload.php'; if (file_exists($packageVendorPath)) { require_once $packageVendorPath; } ... } }
If there is better solution please inform me. I want it to be as simple as possible for customers to set up.
Thanks and best regards.
-
Hello @Okumura
Kindly make sure you have installed all the required dependencies inside the root vendor.
Thanks & Regards
-
Hi @Rishabh-Webkul
Dependencies doesn't exist in root vendor but as I understand based on article: "https://forums.bagisto.com/topic/3288/custom-package-dependencies" it shouldn't be.As I understand packages should have separate dependencies and be completely independent. If I install package at root level for example:
composer require stichoza/google-translate-php
customer will need to do the same thing. If I use few libraries it could be a long list of instructions how to set up my package and it wouldn't be modular solution. My current solution seems to be the simplest option for now.
Thanks and best regards.