Php laravel中引导和引导的区别
我试图了解boot和booted的用法和区别Php laravel中引导和引导的区别,php,laravel,Php,Laravel,我试图了解boot和booted的用法和区别 namespace App; use Illuminate\Database\Eloquent\Model; class User extends Model { protected static function boot() { parent::boot(); static::creating(function ($user) { }); } protected s
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
protected static function boot() {
parent::boot();
static::creating(function ($user) {
});
}
protected static function booted() {
parent::booted();
static::creating(function ($user) {
});
}
}
这两个函数应该在何时何地调用?我尝试了这两个函数,我认为它们是一样的;只是
boot
将首先运行,它需要调用额外的parent::boot()
受保护的静态函数启动()
{
父::boot();
self::creating(函数(Outlet$Outlet):void{
日志::调试(“启动”);
$outlet->name=“boot”;
});
}
受保护的静态函数启动()
{
self::creating(函数(Outlet$Outlet):void{
日志::调试(“启动”);
$outlet->name=“booted”;
});
}
事实上,答案就在雄辩的模型中:
protected function bootIfNotBooted()
{
if (! isset(static::$booted[static::class])) {
static::$booted[static::class] = true;
$this->fireModelEvent('booting', false);
static::booting();
static::boot();
static::booted();
$this->fireModelEvent('booted', false);
}
}
/**
* Perform any actions required before the model boots.
*
* @return void
*/
protected static function booting()
{
//
}
/**
* Bootstrap the model and its traits.
*
* @return void
*/
protected static function boot()
{
static::bootTraits();
}
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
//
}
是的副本吗?如果我没记错的话,在light/Database/Eloquent/Model.php中使用的引导模型的静态数组中引导,在用于引导traits的模型中使用引导方法。