Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

revgov's avatar

Can't register my custom package's dependency in Laravel5.3

  • I am developing a blog as a custom package for Laravel 5.3.
  • So far I have got the routes, controller, models and migrations working.
  • I am now working on the CRUD. For the forms I thought I could use this: https://laravelcollective.com/docs/5.3/html
  • I have installed the package (laravelcollective) with composer.
  • The dependency is in the composer.json file. The files are in my package's vendor folder. I have also tried to run composer dump-autoload -o

Here is my composer.json for my blog package.

{
  "name": "revalgovender/laravel-blog",
  "description": "This is a simple blog for Laravel5",
  "license": "MIT",
  "authors": [
    {
      "name": "Reval Govender",
      "email": "reval.govender23@gmail.com"
    }
  ],
  "autoload": {
    "psr-4": {
      "RevalGovender\\LaravelBlog\\": "app/"
    }
  },
  "require-dev": {
    "phpunit/phpunit": "^5.5"
  },
  "require": {
    "laravelcollective/html": "5.3.*"
  }
}

I have looked everywhere and people suggest to do the following:

public function register()
{
    // Bind the app.
    $this->app->bind('blog', function ($app) {
        return new Blog;
    });

    // Register LaravelCollective Form Builder.
    $this->app->register('Collective\Html\HtmlServiceProvider');
    $this->app->alias('Form', 'Collective\Html\FormFacade');
    $this->app->alias('Html', 'Collective\Html\HtmlFacade');
}

I currently get the following error everywhere:

FatalThrowableError in Application.php line 610: Class 'Collective\Html\HtmlServiceProvider' not found Really not sure where I am going wrong.

I have tried bind instead of register:

$this->app->bind('Collective\Html\HtmlServiceProvider');

But this time I get the following error: Class 'Collective\Html\FormFacade' not found

I have spent 3hours trying to resolve this. I have even opened a StackOverflow post and no one can help with this. http://stackoverflow.com/questions/39985015/cant-register-my-custom-packages-dependency-in-laravel5-3

0 likes
4 replies
willvincent's avatar

I may be wrong, but I believe you'd still have to explicitly include the htmlservice provider in the providers array in config/app.php.

.. eh, maybe not. Looking at https://github.com/laravel-notification-channels/onesignal, which depends on https://github.com/berkayk/laravel-onesignal it looks like the magic that makes it work is:

public function boot() {
       $this->app->when(OneSignalChannel::class) // <- part of first package
            ->needs(OneSignalClient::class) // <- class needed from laravel-onesingal package it depends on.
            ->give(function () { // rest of the boot function logic happens here.. 
revgov's avatar

Thanks for the reply. I don't this is the correct way to go about it. It needs to be done in register() method and not the boot().

dmeganoski@gmail.com's avatar

@revgov just a though, try this...

'use' the file at the top of your class, and then reference it to get it's class name. Like this.

use Collective\Html\HtmlServiceProvider;
use Collective\Html\FormFacade;
use Collective\Html\HtmlFacade;
class ServiceProvider {
    public function register() {
        // Bind the app.
        $this->app->bind('blog', function ($app) {
            return new Blog;
        });
        
        // Register LaravelCollective Form Builder.
        $this->app->register(HtmlServiceProvider::class);
        $this->app->alias('Form', FormFacade::class);
        $this->app->alias('Html', HtmlFacade::class);
    }
}
revgov's avatar
revgov
OP
Best Answer
Level 1

@dmeganoski After speaking to my mentor I have discovered two ways to go about this.

It seems developing packages is a chicken an egg situation. In the "real world", your package will be install with composer and all the dependencies will be install by composer. Unfortunately, while developing a package, you can't really do that. You should register your facades and services providers in your package's service provider as I did.

Short Term Fix: In your main Laravel composer.json map your dependencies in the PSR-4 section so the main project knows where they are. However, this is not ideal.

Long Term Fix: The moment your package is at a decent state, push it to Github and place it in Packagist. Tag it as dev/unstabl/0.0.1. Then pull the package in via composer. Your dependencies will install as normal and you will be good.

Please or to participate in this conversation.