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 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题