Reputation: 1234
Create a package (shopping cart) for my laravel app. Got a service provider - CartServiceProvider
, belongs to namespace Alexxosipov\Cart
.
<?php
namespace Alexxosipov\Cart;
use Illuminate\Support\ServiceProvider;
use Alexxosipov\Cart\Cart as Cart;
class CartServiceProvider extends ServiceProvider {
public function boot() {
}
public function register() {
$this->app->singleton('cart', function() {
return new Cart();
});
}
}
Then, I create new namespace in root composer.json
:
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"Alexxosipov\\Сart\\": "packages/alexxosipov/cart/src"
}
}
And I add it to array providers
in app.php
:
Alexxosipov\Cart\CartServiceProvider::class,
And make composer dump-autoload
. But I still have an error:
FatalErrorException in ProviderRepository.php line 146:
Class 'Alexxosipov\Cart\CartServiceProvider' not found
Where did I go wrong?
UPD: screenshot of my file system
Upvotes: 3
Views: 2811
Reputation: 1702
You can also try this.. Adding the extra key value section on the composer.json file for you package helps with Laravel Class Auto Discovery. https://laravel-news.com/package-auto-discovery
"autoload": {
"psr-4": {
"App\\": "app/",
"Folder\\SubFolder\\": "src"
}
},
"extra": {
"laravel": {
"providers": [
"Folder\\SubFolder\\ClassNameServiceProvider"
],
"aliases": {
"Nickname": "Folder\\SubFolder\\Facades\\ClassName"
}
}
}
Upvotes: 0
Reputation: 807
Try:
"Alexxosipov\\": "packages/alexxosipov/"
Instead of:
"Alexxosipov\\Сart\\": "packages/alexxosipov/cart/"
Then... save your serviceProvider class inside of "packages/alexxosipov/cart/", run compose dumpautoload
command and update your service provider:
<?php
namespace Alexxosipov\Cart;
use Illuminate\Support\ServiceProvider;
use Alexxosipov\Cart\Cart as Cart;
class CartServiceProvider extends ServiceProvider {
public function boot() {}
public function register() {
$this->app->singleton('cart', function() {
return new Cart();
});
}
}
This will must be work fine.
Upvotes: 1