laravel中App Service Provider类的启动和注册方法之间的区别 [英] Difference between boot and register method of App Service Provider class in laravel

查看:370
本文介绍了laravel中App Service Provider类的启动和注册方法之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Laravel的新手,试图了解App Service Provider类中的boot()方法和register()方法之间的区别.我到处搜索,但找不到清晰的答案.衷心感谢您提供更好的解释和友好的合作.

I am new to Laravel and trying to understand the difference between the boot() method and the register() method in App Service Provider class. I have searched all over but haven't been able to find a clear answer. I will really be grateful for a better explanation and kind cooperation.

推荐答案

在注册所有提供程序之后,将对其进行引导".这将在每个提供程序上触发启动方法.使用服务提供者时的一个常见错误是尝试使用register方法中另一提供者提供的服务.由于在register方法内,我们不保证所有其他提供程序都已加载,因此您尝试使用的服务可能尚不可用.因此,使用其他服务的服务提供者代码应始终存在于引导方法中.您猜想,register方法仅应用于在容器中注册服务.在启动方法中,您可以执行任何操作:注册事件侦听器,包括路由文件,注册过滤器或您可以想象的其他任何事情."

"After all providers have been registered, they are "booted". This will fire the boot method on each provider. A common mistake when using service providers is attempting to use the services provided by another provider in the register method. Since, within the register method, we have no gurantee all other providers have been loaded, the service you are trying to use may not be available yet. So, service provider code that uses other services should always live in the boot method. The register method should only be used for, you guessed it, registering services with the container. Within the boot method, you may do whatever you like: register event listeners, include a routes file, register filters, or anything else you can imagine."

因此,寄存器1仅用于绑定.引导程序实际上是触发某些事情的发生.

So the register one is just for binding. The boot one is to actually trigger something to happen.

这篇关于laravel中App Service Provider类的启动和注册方法之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆