0

I am playing with the Laravel custom package development with the Wheniwork composer package. This package doesn't use any namespace. So, I am confused on how can I use it on my custom Laravel package? I am not sure if I'd need to use require() on my service provider but that doesn't looks clean.

This is what I have in my custom service provider:

public function register()
{
    $this->app->singleton('wheniwork', function () {
        return new Wheniwork();
    });
}

It looks Laravel isn't detecting the Wheniwork() class. If I try to dd() the class from the custom web.php file in my package like the following, it shows class not found.

Route::get('/contact', function () {
    var_dump(new Wheniwork());
});

This could be cause the composer package which I'm requiring through my composer.json file from my custom Laravel package has no namespace on it. Any idea what am I doing wrong or what would be the correct way to go about doing this?

Laravel composer.json file:

"autoload-dev": {
    "psr-4": {
        "Tests\\": "tests/",
        "Vendor\\Author\\": "packages/vendor/wheniwork/src/"
    }
}

And this is my custom Laravel package composer.json file:

"autoload": {
    "psr-4": {
        "Vendor\\Wheniwork\\": "src"
    }
}
Md Mazedul Islam Khan
  • 5,318
  • 4
  • 40
  • 66
  • No namespace shouldn't matter as long as the package has the proper autoloading config in their composer.json file. Did you copy this package or install it through composer? – Devon Bessemer Aug 07 '18 at 20:07
  • I just created a `packages` folder on the root directory on where Laravel is installed on my localhost and inside the `packages` folder, I structured my packages folders and files. I autoloaded the `src` directory from the package `composer.json` and also autoloaded the custom package from the Laravel `composer.json` file. I tested the custom package by creating a `web.php` with a route and can confirm the works. BTW, I have installed the package dependencies inside the packages. – Md Mazedul Islam Khan Aug 07 '18 at 20:10
  • Why wouldn't you just install the package through composer? I'm confused as to what you are doing. – Devon Bessemer Aug 07 '18 at 20:13
  • Are you referring the custom Laravel package that I am playing with? If yes, it's a custom package which I just created and not available on packagist. So, this cannot be installed through composer. However, if you are referring the Wheniwork composer package, then yes, I have installed it through composer on my package. – Md Mazedul Islam Khan Aug 07 '18 at 20:16
  • Ok. So I assume the problem is you have two different vendor directories, one for Laravel, and one in your package directory. You need to set up your main composer file to use your package so it includes those dependencies. – Devon Bessemer Aug 07 '18 at 20:22
  • When I run `composer install` from the root of Laravel, it doesn't install my package dependencies. For your convenience, I have listed both of my `composer.json` files in the question. – Md Mazedul Islam Khan Aug 07 '18 at 20:26
  • 1
    Yes, because composer doesn't know your package has a composer.json file and dependencies. You need to set it up as a local repository and include your package in the require section. – Devon Bessemer Aug 07 '18 at 20:27

0 Answers0