Concrete5 5.7: using a ServiceProvider cross-packa

2019-07-21 17:54发布

I'm migrating old code to 5.7, and I'm running into a weird issue when it comes to helpers. I'm working with two packages - let's call them A and B. Package A one implements a ServiceProvider. I would like to use it in a controller in package B, but it seems unable to find it properly. I'm configuring it in the install() function of package A, as follows

$providers = Config::get('providers');
if (!$providers) {
    $providers = array('group_membership' => '\Concrete\Package\A\Src\GroupMembership\GroupMembershipProvider');
} else {
    $providers['group_membership'] = '\Concrete\Package\A\Src\GroupMembership\GroupMembershipProvider';
}
Config::set('providers', $providers);

And in the ServiceProvider itself, I bind it accordingly;

<?php namespace Concrete\Package\A\Src\GroupMembership;

use \Concrete\Core\Foundation\Service\Provider as ServiceProvider;

class GroupMembershipProvider extends ServiceProvider
{
    public function register() {
        $this->app->bind('helper/group_membership', 'Concrete\Package\A\Src\GroupMembership\GroupMembershipProvider');
    }
}

Then when I want to use in package A, this works just fine:

$helper = Core::make('helper/group_membership');

But when I do that in package B, it tells me:

Class helper/group_membership does not exist

What could I do, except for merging the two packages together? What may be causing this?

1条回答
放我归山
2楼-- · 2019-07-21 18:12

Providers cannot be registered like that since we consume that config entry way before we load packages.

The proper way to register a package is to instantiate a new \Concrete\Core\Foundation\Service\ProviderList and use the registerProvider method.

Here's a good example for how to do that

// Register our service provider
$list = new ProviderList(\Core::getFacadeRoot());
$list->registerProvider('Concrete\\Package\\LegacySample\\Libraries\\ServiceProvider');
查看更多
登录 后发表回答