php – auto load package from folder using composer extra – laravel-ThrowExceptions

Exception or error:

i have a “packages” folder inside my laravel application for putting private packages inside it, and the packages structure is

/var/www
  /laravelApp
    /packages
      /company-name
        /package-folder-name
          /src
            /providers
              /MainProvider.php
            /PackageNamespace 
              classCode1.php
              classCode2.php
          composer.json 

and the mainProvider.php code:

<?php

namespace CompanyName\PackageNamespace\Providers;

use Illuminate\Support\ServiceProvider;

class MainProvider extends ServiceProvider
{

    public function register()
    {
        dd("register");
    }

    public function boot()
    {

        dd("boot");
    }

    public function provides()
    {
        return [MainProvider::class];
    }
}

and i’m trying to auto-load the package using "extra" -> "laravel" -> "providers" in the package composer.json like

{
  ...
  "autoload": {
    "psr-4": {
      "CompanyName\\PackageNamespace\\": "src/"
    },
    "autoload-dev": {
      "psr-4": {
        "CompanyName\\PackageNamespace\\": "src/",
        "Tests\\": "tests/"
      }
    }
  },
  "extra": {
    "laravel": {
      "providers": [
        "CompanyName\\PackageNamespace\\Providers\\MainProvider"
      ],
      "aliases": {
        "MainProviderFaced": "Compredict\\WearAlgo\\Managers\\Facades\\WearAlgoManagerFaced"
      }
    }
  }
}

and in the main larvel application composer.json

{
  "name": "laravel/laravel",
  "type": "project",
  "description": "The Laravel Framework.",
  "keywords": [
    "framework",
    "laravel"
  ],
  "license": "MIT",
  "require": {
    "package-name": "dev-master"
  },
  "require-dev": {
  },
  "config": {
    "optimize-autoloader": true,
    "preferred-install": "dist",
    "sort-packages": true
  },
  "extra": {
    "laravel": {
      "dont-discover": []
    }
  },
  "autoload": {
    "classmap": [
      "database/seeds",
      "database/factories"
    ],
    "psr-4": {
      "App\\": "app/",
      "CompanyName\\PackageNamespace\\": "packages/company-name/package-folder-name/src"
    }
  },
  "autoload-dev": {
    "psr-4": {
      "Tests\\": "tests/"
    }
  },
  "minimum-stability": "dev",
  "prefer-stable": true,
  "scripts": {
    "post-autoload-dump": [
      "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
      "@php artisan package:discover --ansi"
    ],
    "post-root-package-install": [
      "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
    ],
    "post-create-project-cmd": [
      "@php artisan key:generate --ansi"
    ]
  },
  "repositories": [
    {
      "type": "path",
      "url": "./packages/company-name/package-folder-name/"
    }
  ]
}

then i do

php artisan clear-compiled 
composer dump-autoload
php artisan optimize
php artisan vendor:publish  --provider="CompanyName\PackageNamespace\Providers\mainProvider" --force

but not working the “register” method of the Package is not being called at all please help thanks in advance.

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *