【问题标题】:Laravel 5.4 Service Provider not loads after site opened站点打开后未加载 Laravel 5.4 服务提供商
【发布时间】:2018-07-18 16:17:44
【问题描述】:

我在 Laravel 5.4 中有项目,我为他们创建了新的自定义类和服务提供者。并将此类绑定到服务提供者register 方法中的应用程序。并在 app.php 配置中编写此提供程序。但是在加载应用程序时它不会加载。我在自定义类 Helper__construct 方法中写了 Log,但是日志没有写入 laravel.log 文件,并且其他操作没有在这个 __construct 方法中执行。以下来自服务提供商的代码。

public function register()
{
    App::bind('helper', function() {
        return new Helper;
    });
}

【问题讨论】:

  • 而在自定义类中,__construct 中的 Auth 不能正常工作,它可能是针对中间件的?

标签: php laravel


【解决方案1】:

为了确定,您在config/app.php 中注册了提供商,例如:

'providers' => [
    ...
    Full\Path\To\ClassServiceProvider::class,
    ...
 ]

绑定的时候应该是这样的:

public function register()
{
    App::bind('Full\Path\To\Class', function() {
        return new Helper;
    });
}

然后您可以将 helper 别名为您的班级全名:

App:alias('helper', 'Full\Path\To\Class');

【讨论】:

    【解决方案2】:

    你不需要绑定一个类或者创建一个别名,它会自动注册到容器中。只需创建一个类,运行composer du 命令并使用其完整的类名(包括命名空间)注入该类:

    app('\App\SomeNamespace\Helper');
    

    或者使用控制器构造函数,例如:

    use App\SomeNamespace\Helper;
    public function __construct(Helper $helper)
    

    如果你想将它绑定到 helper 之类的东西上,你可以这样做:

    app()->bind('helper', '\App\SomeNamespace\Helper');
    

    然后使用它:

    app('helper');
    

    【讨论】:

      猜你喜欢
      • 2016-11-08
      • 1970-01-01
      • 2016-08-23
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 2017-08-20
      • 1970-01-01
      • 2017-07-26
      相关资源
      最近更新 更多