i'm a newbie at composer, so just bear with me,
So i have a package, which i'm loading from local folder, and while using it, i get the following error:
Fatal error: Class 'mypkg\Layer\EasyCPT' not found in
C:\xampp\htdocs\testwp\app\Cpt\location.php on line 5
My Composer.json:
"repositories": [
{
"type":"vcs",
"url":"C:/xampp/htdocs/mypkg"
}
],
"require": {
"php": ">=7.0.0",
"mypkg/particles": "master"
},
"autoload": {
"psr-4": {
"App\\": "app/"
}
}
Package's Composer:
"minimum-stability": "dev",
"authors": [
{
"name": "Talha Abrar",
"email": "talha#themegeek.io"
}
],
"autoload": {
"psr-4": {
"Mypkg\\": "particles/"
}
}
Psr 4:
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Mypkg\\' => array($vendorDir . '/Mypkg/particles/particles'),
'App\\' => array($baseDir . '/app'),
);
how i am using it:
<?php
namespace App\Cpt;
use Mypkg\Layer\EasyCPT;
class Location extends EasyCPT{
protected $plural = 'locations';
}
Main auto-loading file:
require __DIR__.'/vendor/autoload.php';
use App\Init\EasyWP;
new EasyWP();
You use the namespace as:
use Particles\Layer\EasyCPT;
but in autoload section defining as:
"Mypkg\\": "particles/"
which is not consistent.
You should replace Mypkg with the right namespace name, e.g.
"autoload": {
"psr-4": {
"Particles\\": "particles/"
}
}
So requesting Particles\Layer\EasyCPT namespace will look for class in particles/Layer/EasyCPT.php file.
As per Composer's PSR-4 documentation:
Under the psr-4 key you define a mapping from namespaces to paths, relative to the package root. When autoloading a class like Foo\\Bar\\Baz a namespace prefix Foo\\ pointing to a directory src/ means that the autoloader will look for a file named src/Bar/Baz.php and include it if present. Note that as opposed to the older PSR-0 style, the prefix (Foo\\) is not present in the file path.
If your project doesn't follow PSR-4 approach, use classmap instead to scan for all of your classes, e.g.
"autoload": {
"classmap": ["particles/"],
"exclude-from-classmap": ["/tests/"]
}
To regenerate autoload manually, run:
composer dump-autoload -o
and check autoload files in vendor/composer/ whether the references to classes were generated correctly.
Related
i've already been looking for solutions to my problem but could'nt find any so far.
{
"name": "petersil98/thresh",
"version": "1.0.0",
"type": "library",
"autoload": {
"psr-4": {
"Thresh\\": "src/"
}
}
}
This is my public/test.php:
<?php
require_once '../vendor/autoload.php';
use Thresh\Helper\Config;
Config::setPlatform("euw1");
And this is my src/Helper/Config.php:
<?php
namespace Thresh\Helper;
class Config{...}
This is the error i get: Fatal error: Uncaught Error: Class 'Thresh\Helper\Config' not found
First i had my psr-4 autoload registered with the prefix 'src'
"autoload": {
"psr-4": {
"src\\": "src/"
}
}
After changing the psr-4 autoload prefix to Thresh (and updating the namespaces) and running composer dump-autoload it doesnt work anymore
PS: composer dump-autoload returns Generated autoload files containing 0 classes
I'm using Laravel modules by nwidart and I want to create module in subfolder. According issue on its Github this is not possible but is possible to change paths in composer.json. So I've created new module Role2 and moved it to subfolder Administration. Then I've changed module's composer.json:
{
"name": "nwidart/role2",
"description": "",
"authors": [
{
"name": "Nicolas Widart",
"email": "n.widart#gmail.com"
}
],
"extra": {
"laravel": {
"providers": [
"Modules\\Administration\\Role2\\Providers\\Role2ServiceProvider"
],
"aliases": {
}
}
},
"autoload": {
"psr-4": {
"Modules\\Administration\\Role2\\": ""
}
}
}
But after command composer dumpautoload I see nothing in command php artisan module:list and also in vendor/composer/autoload_psr4.php there is no path to this module. What I'm doing wrong or what is missing to generate multiple composer (one in root and one in module)? Or is there some simple option to have module in subfolder?
There is what I have in root composer.json:
...
"autoload": {
"classmap": [
"database",
"vendor",
"app/Easyk"
],
"exclude-from-classmap": [
"vendor/swiftmailer"
],
"psr-4": {
"App\\": "app",
"Modules\\": "Modules",
"Easyk\\": "app/Easyk",
"Asipem\\OAuth2ClientManagement\\": "packages/asipem/oauth2-client-management/src",
"Asipem\\CAPRequestDispatcher\\": "packages/asipem/cap-request-dispatcher/src"
}
},
"autoload-dev": {
"classmap": [
"tests/TestCase.php"
],
"psr-4": {
"Tests\\Unit\\": "tests/unit"
}
},
...
autoload -> psr-4 key from composer.json is used to define psr-4 root namespaces for the application.
What happens is you define a namespace and the directory location for Autoloader to look for the files under that namespace.
As in your root composer.json
"psr-4": {
"App\\": "app",
"Modules\\": "Modules",
"Easyk\\": "app/Easyk",
"Asipem\\OAuth2ClientManagement\\": "packages/asipem/oauth2-client-management/src",
"Asipem\\CAPRequestDispatcher\\": "packages/asipem/cap-request-dispatcher/src"
}
App is the base namespace and app is its base directory. By defining it, you tell autoloader that all files with namespace App will be residing inside the directory 'app'. After that, Autoloader will traverse the app directory in the PSR-4 way for file path following the namespace trail after base namespace App.
For example: Controller App\Controller\ExampleController will tell Autoloader that the class defination will be found at app/Controller/ExampleController.
So you need to specify the base directory location of your module against the namespace Modules\\Administration\\Role2\\ which is the base source directory of your module folder.
Hope it helped.
I've created a TYPO3 7.6 extension with the extension builder. Now I want to add and autoload composer installed packages. I read that ext_autoload.php won't be loaded since TYPO3 version 7 in general. For this you should use the ext_emconf.php OR the composer.json in the root path of your extension.
So I've setup the following composer.json and installed it with the composer.phar.
{
"require": {
"hybridauth/hybridauth": "v3.0.0-rc.1"
},
"config": {
"vendor-dir": "Vendor"
},
"autoload": {
"psr-4": {
"Vendor\\Package\\": "Classes"
},
"classmap": [
"Vendor/hybridauth/hybridauth/src"
]
}
}
Now there is the Vendor/hybridauth/hybridauth/src as expected but the autoloader of TYPO3 doesn't find for example the Vendor/hybridauth/hybridauth/src/Hybridauth.php defined class Hybridauth. I've checked it with:
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump(get_declared_classes());
What did I miss? What do I have to add/change/rethink?
Your json file content like below.
{
"require": {
"hybridauth/hybridauth": "v3.0.0-rc.1"
},
"config": {
"vendor-dir": "Vendor"
},
"autoload": {
"psr-4": {
"vendor\\extensionName\\": "Classes"
},
"classmap": [
"Vendor/hybridauth/hybridauth/src/"
],
"files": [
"Vendor/hybridauth/hybridauth/src/Hybridauth.php"
]
}
}
It was my fault. TYPO3 already autoloads already the psr-4 called environments. But I had to have add the autoloader of Hybridauth itself as well like:
<?php
namespace Vendor\Package\Service;
require_once dirname(dirname(dirname(__FILE__))) . '/Vendor/autoload.php';
class SomeService extends \TYPO3\CMS\Sv\AbstractAuthenticationService{
// ...
}
I'm starting work on a new mini-framework project, which I have in a local GIT repo on my machine. I've set up a test project that pulls in the local repo via Composer, however the autoloader isn't working as expected (Fatal Error: Class X not found errors). This is the first time I've used autoloading outside of what is automatically generated (e.g. when using an existing framework) and despite reading around, I can't seem to solve this.
Package
In an attempt to get this working, the package only contains a src directory with a single App.php class on top of the composer.json file in the root.
composer.json
{
"name": "myvendor/framework",
"description": "Framework Description",
"license": "MIT",
"authors": [
{
"name": "Joe Bloggs",
"email": "joe#email.com"
}
],
"autoload": {
"psr-0": {
"Framework": "src/"
}
}
}
Project
composer.json
{
"repositories": [
{
"type": "vcs",
"url" : "../Framework"
}
],
"require": {
"myvendor/framework": "dev-master"
}
}
This successfully clones the local repo and adds the code to the vendor directory.
The namespace is also successfully added to Composer's autoload_namespaces.php file like so;
vendor/composer/autoload_namespaces.php
'Framework' => array($vendorDir . '/myvendor/framework/src'),
When I attempt to load the App class however using the following code, I get the error;
web/index.php
<?php
require_once '../vendor/autoload.php';
$app = new \Framework\App();
You're using the psr-0 specification for the class loader. This means that the full namespace has to be visible in the file structure. The prefix only tells the autoloader were to look for this namespace.
So in your case, you configured that the "Framework" namespace is available in the "src/" directory. This means that the class \Framework\App should life in src/Framework/App.php. In your case, it exists in src/App.php. This means that the autoloader cannot find your class.
However, there is a class loader specification that does what you want: psr-4. This is also the recommended specification (psr-0 might be removed in the future). With PSR-4, the file structure only includes the namespaces after the configured prefixes. So when doing "psr-4": { "Framework\": "src/" }, a class called \Framework\App should life in src/App.php and a class called \Framework\Some\Special\App should life in src/Some/Special/App.php.
I have create a custom composer package but I am having troubles to set the correct autoload options for it.
All my classes are under MyNamespace/Common namespace. So for example for including my ArrayHelper class I do use Mynamespace/Common/Helper/ArrayHelper.
This is the relevant part of my composer.json:
"autoload": {
"psr-0": { "MyNamespace\\": "" }
}
I have read this: composer.json / autoload
Any help?
You have to navigate the file location of your namespace.
"autoload": {
"psr-0": { "MyNameSpace": "./<path to your parent directory>" }
}
For example, this is my directory structure:
composer.json
source
\-Data
|-Controller
\-Repository
Then, in the composer.json file:
"autoload": {
"psr-0": { "MyNameSpace": "source/Data" }
}
Then, I can define classes in these namespaces:
/* namespace for classes in controller directory */
namespace MyNameSpace\Controller;
/* namespace for classes in repository directory */
namespace MyNameSpace\Repository;