我正在将旧代码迁移到5.7,而且当涉及到帮助程序时,我遇到了一个奇怪的问题。我正在使用两个包 - 让我们称它们为A和B.包A实现一个ServiceProvider。我想在包B中的控制器中使用它,但它似乎无法正确找到它。我在包A的install()
函数中配置它,如下所示
$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);
在ServiceProvider本身,我相应地绑定它;
<?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');
}
}
然后当我想在包A中使用时,这很好用:
$helper = Core::make('helper/group_membership');
但是当我在包B中这样做时,它告诉我:
Class helper/group_membership does not exist
除了将两个包合并在一起外,我该怎么办?可能导致这种情况的原因是什么?
答案 0 :(得分:2)
自we consume that config entry之前的we load packages以来,提供商无法像这样注册。
注册包的正确方法是实例化新的\Concrete\Core\Foundation\Service\ProviderList
并使用registerProvider
方法。
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');