Marco Gasi
asked on
SOmething is wrong in composer.json autoloading my own packages
Hi everybody.
So I have the following directory structure:
webroot
- admin
- vendor
- myPackage
- MyClasses1
- MyFirstClass.php
- MyClasses2
- autoload.php
- composer.json
- css
- js
- vendor
- composer.json
- index.php
This is my composer.json in admin directory:
And this is my composer.json in webroot:
In MyFirstClass.php I have
And I relize that I have even the problem to use Stripe and Paypal essentially in website's pages so I nned to include them someway in my autoload...
Okay, totally lost.
My main problem is that I would like to have my custom CMS ready to add or not some module and once I add a module I would just add some lines to the composer.json for the website where the admin directory with my CMS is.
Please, reveal my certainly stupid mistake because I can't guess it...
Thank you in advance for any help.
So I have the following directory structure:
webroot
- admin
- vendor
- myPackage
- MyClasses1
- MyFirstClass.php
- MyClasses2
- autoload.php
- composer.json
- css
- js
- vendor
- composer.json
- index.php
This is my composer.json in admin directory:
{
"require": {
"paypal/rest-api-sdk-php": "1.13",
"stripe/stripe-php": "^6.31",
"twig/twig": "^2.0"
},
"autoload": {
"psr-4": {
"MyPackage\\": "./admin/vendor/MyPackage/MyClasses1"
}
}
}
And this is my composer.json in webroot:
{
"autoload": {
"psr-4": {
"MyPackage\\": "./admin/vendor/MyPackage/MyClasses1"
}
}
}
In MyFirstClass.php I have
namespace MyClasses1;
require __DIR__ . '/../../autoload.php';
In my index.php I have:require $_SERVER['DOCUMENT_ROOT'] . '/admin/vendor/autoload.php';
use MyPackage\MyClasses1\MyFirstClass;
$MyObject = new MyFirstClass();
But something is wrong and I'm bit lost. I get the errorUncaught Error: Class 'MyPackage\MyClasses1\MyFirstClass' not found
And I relize that I have even the problem to use Stripe and Paypal essentially in website's pages so I nned to include them someway in my autoload...
Okay, totally lost.
My main problem is that I would like to have my custom CMS ready to add or not some module and once I add a module I would just add some lines to the composer.json for the website where the admin directory with my CMS is.
Please, reveal my certainly stupid mistake because I can't guess it...
Thank you in advance for any help.
Hi, maybe there is an error in the paths..
Hey Marco,
Couple of issues I see. In your admin composer.jon, you seem to have the path wrong. The composer.json file is at the same level as vendor, so you need:
"MyPackage\\": "./vendor/myPackage/MyClas ses1"
That value is a path, so make sure the case matches (doesn't really matter on Windows PC but it will on Linux).
Also, it probably makes sense to drop the MyClasses1 from the path, otherwise you'll need another entry for MyClasses2
You've also set the namespace prefix to MyPackage, so your class namespace should look like:
namespace MyPackage\MyClasses1;
class MyFirstClass {
...
}
Once you've done that, dump your autoload data by running this in your admin folder:
composer dump-autoload -o
That should generate the autoload info and you should be good to go.
FYI - I would go with just one composer.json, one vendor folder and one autoloader. It'll make your like a lot easier.
Couple of issues I see. In your admin composer.jon, you seem to have the path wrong. The composer.json file is at the same level as vendor, so you need:
"MyPackage\\": "./vendor/myPackage/MyClas
That value is a path, so make sure the case matches (doesn't really matter on Windows PC but it will on Linux).
Also, it probably makes sense to drop the MyClasses1 from the path, otherwise you'll need another entry for MyClasses2
You've also set the namespace prefix to MyPackage, so your class namespace should look like:
namespace MyPackage\MyClasses1;
class MyFirstClass {
...
}
Once you've done that, dump your autoload data by running this in your admin folder:
composer dump-autoload -o
That should generate the autoload info and you should be good to go.
FYI - I would go with just one composer.json, one vendor folder and one autoloader. It'll make your like a lot easier.
ASKER
Hi Chris, hope you're doing well :)
Thank you for your answer, but still no luck: even following your suggestions I still get error.
Running composer dump-autoload -o should create an updated vendor/composer/autoload_p sr4.php, isn't it? If so, there's something really confused in my directory structure, because in this file I find only one of my packages and it has wrong values... I have to do a serious cleanup and then I'll come back to you here.
The problem probably arises because I'm trying to put order in an existing project originally built without using composer, nor namespaces.
Hope to come back soon.
Thank you :)
EDIT: I have just realized that dump-autoload -o doesn't change any file in vendor/composer directory...
Thank you for your answer, but still no luck: even following your suggestions I still get error.
Running composer dump-autoload -o should create an updated vendor/composer/autoload_p
The problem probably arises because I'm trying to put order in an existing project originally built without using composer, nor namespaces.
Hope to come back soon.
Thank you :)
EDIT: I have just realized that dump-autoload -o doesn't change any file in vendor/composer directory...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Cleaning additionale instances of composer, reducing to one namespace which include several subnamespaces with their classes and adjusting use clauses here and there fixed the issue. Thank you
Good news Marco. Glad you got it sorted :)