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.
Related
I want to override vendor class EloquentUserProvider.
I have created one same file in app/Ovverides folder
this is my composer.json
"exclude-from-classmap": [
"vendor\\Illuminate\\Auth\\EloquentUserProvider.php"
],
"psr-4": {
"App\\": "app/",
"App\\Ovverides\\": "app/Ovverides/"
}
after running composer dump-autoload , the file is not overridden
I'm want create a packagist based in laravel,
I've created a laravel project,
I've created the next directory:
app/
...
packages/
- imfranq/
-- mypackage/
--- src/
---- Providers/
I want use the provider created in my Providers folder
My package composer.json
...
"autoload": {
"psr-4": {
"Imfranq\\MyPackage\\": "src/"
}
}
laravel app.php
'providers' => [
...
Imfranq\MyPackage\Providers\MyPackageServiceProvider::class,
]
I've run make:provider command and I've moved the file to my package folder
My MyPackageServiceProvider.php
public function boot()
{
dd('test');
}
Up to this point nothing works.
I try:
composer dump-autoload command
composer cache:clear command
php artisan clear-compiled command
nothing works
I try in laravel package.json
"autoload": {
"psr-4": {
"App\\": "app/",
"Imfranq\\MyPackage\\": "packages/imfranq/mypackage/src/"
},
"classmap": [
"packages/imfranq/mypackage/src",
"database/seeds",
"database/factories"
]
},
also
"extra": {
"laravel": {
"providers": [
"Imfranq\\MyPackage\\MyPackageServiceProvider"
]
}
},
Not work...
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 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.
I have an RSA algorithm Library giving to me by a payment gateway and When I do a
include (app_path().'/PaymentGateway/Crypt/RSA.php');
this and try to make an object as $rsa = new Crypt_RSA(); this it gives me and error saying
Class 'App\Http\Controllers\Crypt_RSA' not found
I tried including it in web.php and making an object it worked the problem occur when I try to include it in a Controller.
This is what I did. Oh and a little back ground I use to have this in Laravel 4, PHP 5, jpgraph 2.
I am using jpgraph 4.1 on Laravel 5.5 using PHP 7.
Created a folder under app called jpgraph
Placed the src folder that is in the tarball of jpgraph in that folder
Created file call Graph1.php, is my code using jpgraph, with the class Custom_GraphsJM in the jpgraph folder.
In composer.json added "app/jpgraph/Graph1.php" to the "classmap"
"autoload": {
"classmap": [
"database/seeds",
"database/factories",
"app/jpgraph/Graph1.php"
],
"psr-4": {
"App\\": "app/"
}
},
In the application folder:
composer dump-autoload
Checked the autoload_classmap.php and I have
'Custom_GraphsJM' => $baseDir . '/app/jpgraph/Graph1.php',
In my Model at the top I have
use Custom_GraphsJM;
To create a class
$Two_Graphs_Temp = new Custom_GraphsJM();
You can tell Composer to autoload any (non-PSR) class by adding the base folder to:
"autoload": {
"classmap": [
"app/commands",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
....
And you can also autoload autoloaders by adding them to the files section:
"autoload": {
"files": [
"temboo/src/Temboo_Loader.php"
],
...
After adding those entries, execute:
composer dumpautoload
And check the file vendor/composer/autoload_classmap.php, the available classes must be all listed in it, if one file is not there it will not be autoloaded.
On default, everything included in the app folder of your laravel project is autoloaded, that is described in the composer.json of your project:
...
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
}
},
...
The only thing you will need to do is simply use the namespace:
use App/Path/To/Third/Party/plugin/Class;
If, however, the plugin is placed outside of the scope of App, then simply add it to the psr-4 autoloader:
"psr-4": {
"ProjectRootNs\\": "projects/myproject/"
}