douzhuolong9886 2018-03-13 23:09
浏览 65
已采纳

Laravel Artisan加载定制服务提供商时的错误 - 有没有办法避免这种情况?

I have added a custom Service Provider to a Laravel app and the service provider runs well. However, Artisan gets an error now. When I remove the service provider the error goes away ( no errors when the Service Provider runs in normal mode ). In this case the error is related to the DB Drivers not being loaded. "Driver not found" errors.

Apparently, when running in Artisan mode it still loads all of the Service Providers even though some of the dependancies like DB Drivers, and other dependencies, aren't loaded.

Does anyone know of a way around this? Forcing the dependencies to load or to prevent the offending Service Providers to not load in Artisan mode? Possibly conditional loading of Service Providers would work if I can find a way to detect its running in Artisan CLI mode.

In case it helps here's where/how the Service Provider is registered:

public function register()
{
    $this->app->singleton(Locations::class, function ($app) {
        return new Locations($app->request);
    });
}

Error Message:

 could not find driver (SQL: select * from...

Apparently the offending custom service provider is loading and running before the DB driver resources are loaded and available in Artisan CLI mode - not having this problem in normal browser mode. Either that or Artisan doesn't load the DB drivers at all in CLI mode.

Any feedback would be appreciated.

Thanks in advance.

  • 写回答

2条回答 默认 最新

  • duaijiao0648 2018-03-14 00:15
    关注

    This worked:

    if (\App::runningInConsole()){...
    

    In this case I placed it in the __construct() of the class to return null before it ran bc placing it in the Service Provider file created other unexpected issues. Not elegant but it works seamlessly.

    Thank you to @apokryfos for the suggestion on the runningInConsole() function. Wasn't aware of that one.

    If I find other options I'll post them here. Still digging into custom Service Providers more. A very powerful feature of Laravel that I'm just getting into.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料