Donal.Lynch.Msc
Donal.Lynch.Msc

Reputation: 3615

Laravel Package Creation

So I'm writing a Laravel Package that will eventually exist on git (or wherever).

I've created the package structure as outlined here (https://rathes.de/blog/en/laravel-package, great resource by the way), in /app/ and I've also created a Facade.php, PackageServiceProvoder.php and a controller that dumps "Hello world" to the browser.

I also have a pre-existing HomeController.php and I've added the following to the test function:

dd(\MyPackage::test());// <-- Should dump "Hello world" and die.

I've also added the following to the packages' composer.json file and run composer install:

"autoload": {
    "psr-4": {
        "Company\\LaravelPackageMyPackage\\": "src"
    }
},
"autoload-dev": {
    "psr-4": {
        "Company\\LaravelPackageMyPackage\\Test\\": "tests"
    }
},
"extra": {
    "laravel": {
        "providers": [
"Company\\LaravelPackageMyPackage\\Lar...kageServiceProvider"
        ],
        "aliases": {
            "MyPackage": "Company\\LaravelPackageMyPackage\\Facade"
        }
    }
},

The problem I have is that when I run this in the browser I get the following error:

Class 'MyPackage' not found // <-- references the dd() line of code above

I must have missed a step during the tutorial or something like that because as far as I can see it should be working now. Any suggestions guys?

Laravel 5.5, PHP 7.1, Ubuntu Linux os.

Just code below here:

File structure (very similar to this):
Project -> app -> my-package /

├── database/
│   ├── .gitkeep
├── config/
│   ├── package-name.php
├── src/
│   ├── PackageNameServiceProvider.php
├── tests/
│   ├── TestCase.php
├── .gitignore
├── CHANGELOG.md
├── composer.json
├── LICENSE
├── phpunit.xml
├── README.md

// FACADE:
<?php
namespace Company\LaravelPackageMyPackage;

class Facade extends \Illuminate\Support\Facades\Facade
{
    /**
     * {@inheritDoc}
     */
    protected static function getFacadeAccessor()
    {
        return 'laravelpackagemypackage';
    }
}

// MY PACKAGE
<?php
namespace Company\LaravelPackageMyPackage;

class LaravelPackageMyPackage
{
    public function test()
    {
        return "Hello From My Package!";
    }
}

// SERVICE PROVIDER:
<?php
namespace Company\LaravelPackageMyPackage;
use Illuminate\Support\Facades\App;
use Illuminate\Support\ServiceProvider;

class LaravelPackageMyPackageServiceProvider extends ServiceProvider
{
    protected $defer = false;

    public function boot()
    {
        //$this->loadMigrationsFrom(__DIR__ . '/../database/migrations/');
    }

    public function register()
    {
        App::bind('laravelpackagemypackage', function () {
            return new LaravelPackageMyPackage();
        });
    }
}

Upvotes: 1

Views: 116

Answers (1)

Marcus
Marcus

Reputation: 1848

I had a similar problem.

Make sure that your class namespace is the same as the composer.json In your ServiceProvider and Facade the namespace should be

 namespace Company\LaravelPackageMyPackage

Also check that your Facade is returning the right string

protected static function getFacadeAccessor()
{
    return 'mypackage';
}

In your ServiceProvider register method

App::bind('mypackage', function () {
    return new MyPackage();
});

EDIT

You have written laravelpackageMyPackage but it should be laravelpackagemypackage in your getFacadeAccessor()

Edit When calling MyPackage::test() try adding

use MyPackage;

in your controller if that does not work

try calling the facade directly

Company\LaravelPackageMyPackage\Facade::test()

Upvotes: 1

Related Questions